2025-07-24 20:57:19 -04:00
|
|
|
import bcrypt from 'bcryptjs';
|
|
|
|
|
import jwt from 'jsonwebtoken';
|
|
|
|
|
import { sql } from '@vercel/postgres';
|
2025-07-27 16:17:51 -04:00
|
|
|
import { generateUniqueSlug } from '../../../lib/slug-utils.js';
|
2025-07-24 20:57:19 -04:00
|
|
|
|
|
|
|
|
const JWT_SECRET = process.env.JWT_SECRET || 'your-secret-key-change-in-production';
|
2025-07-23 10:38:16 -04:00
|
|
|
|
|
|
|
|
export default async function handler(req, res) {
|
|
|
|
|
// Set CORS headers
|
|
|
|
|
res.setHeader('Access-Control-Allow-Origin', '*');
|
|
|
|
|
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS');
|
|
|
|
|
res.setHeader('Access-Control-Allow-Headers', 'Content-Type, Authorization');
|
|
|
|
|
|
|
|
|
|
// Handle preflight requests
|
|
|
|
|
if (req.method === 'OPTIONS') {
|
|
|
|
|
res.status(200).end();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (req.method !== 'POST') {
|
|
|
|
|
return res.status(405).json({ error: 'Method not allowed' });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
const { email, password } = req.body;
|
|
|
|
|
|
|
|
|
|
if (!email || !password) {
|
|
|
|
|
return res.status(400).json({ error: 'Email and password are required' });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (password.length < 6) {
|
|
|
|
|
return res.status(400).json({ error: 'Password must be at least 6 characters' });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Check if user already exists
|
2025-07-24 20:57:19 -04:00
|
|
|
const existingUser = await sql`
|
|
|
|
|
SELECT id FROM users WHERE email = ${email}
|
|
|
|
|
`;
|
2025-07-23 10:38:16 -04:00
|
|
|
|
|
|
|
|
if (existingUser.rows.length > 0) {
|
|
|
|
|
return res.status(409).json({ error: 'User already exists' });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Hash password
|
2025-07-24 20:57:19 -04:00
|
|
|
const hashedPassword = await bcrypt.hash(password, 12);
|
2025-07-23 10:38:16 -04:00
|
|
|
|
|
|
|
|
// Create user
|
2025-07-24 20:57:19 -04:00
|
|
|
const result = await sql`
|
2025-07-23 10:38:16 -04:00
|
|
|
INSERT INTO users (email, password, role)
|
2025-07-24 20:57:19 -04:00
|
|
|
VALUES (${email}, ${hashedPassword}, ${'user'})
|
2025-07-23 10:38:16 -04:00
|
|
|
RETURNING id, email, role, created_at
|
2025-07-24 20:57:19 -04:00
|
|
|
`;
|
2025-07-23 10:38:16 -04:00
|
|
|
|
2025-07-24 20:57:19 -04:00
|
|
|
const user = result.rows[0];
|
2025-07-23 10:38:16 -04:00
|
|
|
|
2025-07-27 16:17:51 -04:00
|
|
|
// Create the automatic "All My Cards" collection for the new user
|
|
|
|
|
try {
|
|
|
|
|
// Get existing slugs to ensure uniqueness
|
|
|
|
|
const existingSlugsData = await sql`SELECT slug FROM collections WHERE slug IS NOT NULL`;
|
|
|
|
|
const existingSlugs = (existingSlugsData.rows || []).map(row => row.slug);
|
|
|
|
|
|
|
|
|
|
// Generate unique slug for "All My Cards"
|
|
|
|
|
const uniqueSlug = await generateUniqueSlug("All My Cards", existingSlugs);
|
|
|
|
|
|
|
|
|
|
// Create the special collection
|
|
|
|
|
const collectionResult = await sql`
|
|
|
|
|
INSERT INTO collections (
|
|
|
|
|
name,
|
|
|
|
|
description,
|
|
|
|
|
tcg,
|
|
|
|
|
is_public,
|
|
|
|
|
user_id,
|
|
|
|
|
slug,
|
|
|
|
|
is_system_collection,
|
|
|
|
|
created_at,
|
|
|
|
|
updated_at
|
|
|
|
|
)
|
|
|
|
|
VALUES (
|
|
|
|
|
'All My Cards',
|
|
|
|
|
'Automatically contains all cards you mark as owned. This collection cannot be deleted or made public.',
|
|
|
|
|
'All',
|
|
|
|
|
false,
|
|
|
|
|
${user.id},
|
|
|
|
|
${uniqueSlug},
|
|
|
|
|
true,
|
|
|
|
|
CURRENT_TIMESTAMP,
|
|
|
|
|
CURRENT_TIMESTAMP
|
|
|
|
|
)
|
|
|
|
|
RETURNING id
|
|
|
|
|
`;
|
|
|
|
|
|
|
|
|
|
const collection = collectionResult.rows[0];
|
|
|
|
|
|
|
|
|
|
// Create owner permission for the collection
|
|
|
|
|
await sql`
|
|
|
|
|
INSERT INTO collection_permissions (collection_id, user_id, role, status, created_at)
|
|
|
|
|
VALUES (${collection.id}, ${user.id}, 'owner', 'active', CURRENT_TIMESTAMP)
|
|
|
|
|
`;
|
|
|
|
|
|
|
|
|
|
console.log(`✅ Created "All My Cards" collection for user ${user.email} (ID: ${collection.id})`);
|
|
|
|
|
} catch (collectionError) {
|
|
|
|
|
console.error('Error creating "All My Cards" collection:', collectionError);
|
|
|
|
|
// Don't fail the registration if collection creation fails
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-24 20:57:19 -04:00
|
|
|
// Generate JWT token
|
|
|
|
|
const token = jwt.sign(
|
|
|
|
|
{ userId: user.id, email: user.email, role: user.role },
|
|
|
|
|
JWT_SECRET,
|
|
|
|
|
{ expiresIn: '24h' }
|
|
|
|
|
);
|
2025-07-23 10:38:16 -04:00
|
|
|
|
|
|
|
|
res.status(201).json({
|
|
|
|
|
success: true,
|
|
|
|
|
user,
|
|
|
|
|
token
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error('Registration error:', error);
|
|
|
|
|
res.status(500).json({ error: 'Internal server error' });
|
|
|
|
|
}
|
|
|
|
|
}
|