2025-07-24 20:57:19 -04:00
|
|
|
import bcrypt from 'bcryptjs';
|
2026-08-15 10:32:13 -04:00
|
|
|
import { sql } from '../../../lib/sql.js';
|
2025-07-27 16:17:51 -04:00
|
|
|
import { generateUniqueSlug } from '../../../lib/slug-utils.js';
|
2026-05-29 11:01:03 -04:00
|
|
|
import { SYSTEM_COLLECTION_DB_NAME, VOCAB } from '../../../lib/collection-vocabulary.js';
|
2026-05-23 10:47:11 -04:00
|
|
|
import { generateToken } from '../auth-utils.js';
|
2026-05-23 11:51:33 -04:00
|
|
|
import { checkAuthRateLimit } from '../../../lib/rate-limit.js';
|
2025-07-23 10:38:16 -04:00
|
|
|
|
|
|
|
|
export default async function handler(req, res) {
|
|
|
|
|
if (req.method !== 'POST') {
|
|
|
|
|
return res.status(405).json({ error: 'Method not allowed' });
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-23 11:51:33 -04:00
|
|
|
const { allowed, reset } = await checkAuthRateLimit(req);
|
|
|
|
|
if (!allowed) {
|
|
|
|
|
res.setHeader('Retry-After', Math.ceil((reset - Date.now()) / 1000));
|
|
|
|
|
return res.status(429).json({ error: 'Too many attempts. Try again later.' });
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-23 10:38:16 -04:00
|
|
|
try {
|
2025-07-28 12:18:58 -04:00
|
|
|
const { email, password, firstName, lastName, username, profileImage } = req.body;
|
2025-07-23 10:38:16 -04:00
|
|
|
|
2025-07-28 12:18:58 -04:00
|
|
|
// Validate required fields
|
|
|
|
|
if (!email || !password || !firstName || !lastName || !username) {
|
|
|
|
|
return res.status(400).json({ error: 'All fields are required' });
|
2025-07-23 10:38:16 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (password.length < 6) {
|
|
|
|
|
return res.status(400).json({ error: 'Password must be at least 6 characters' });
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-28 12:18:58 -04:00
|
|
|
// Validate username
|
|
|
|
|
if (username.length < 3) {
|
|
|
|
|
return res.status(400).json({ error: 'Username must be at least 3 characters' });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!/^[a-zA-Z0-9_]+$/.test(username)) {
|
|
|
|
|
return res.status(400).json({ error: 'Username can only contain letters, numbers, and underscores' });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Check if user already exists (email or username)
|
2025-07-24 20:57:19 -04:00
|
|
|
const existingUser = await sql`
|
2025-07-28 12:18:58 -04:00
|
|
|
SELECT id FROM users WHERE email = ${email} OR username = ${username}
|
2025-07-24 20:57:19 -04:00
|
|
|
`;
|
2025-07-23 10:38:16 -04:00
|
|
|
|
|
|
|
|
if (existingUser.rows.length > 0) {
|
2025-07-28 12:18:58 -04:00
|
|
|
// Check which field conflicts
|
|
|
|
|
const conflictUser = await sql`
|
|
|
|
|
SELECT email, username FROM users WHERE email = ${email} OR username = ${username}
|
|
|
|
|
`;
|
|
|
|
|
|
|
|
|
|
const conflict = conflictUser.rows[0];
|
|
|
|
|
if (conflict.email === email) {
|
|
|
|
|
return res.status(409).json({ error: 'Email already exists' });
|
|
|
|
|
} else {
|
|
|
|
|
return res.status(409).json({ error: 'Username already taken' });
|
|
|
|
|
}
|
2025-07-23 10:38:16 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 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
|
|
|
|
2025-07-28 12:18:58 -04:00
|
|
|
// Create user with all fields
|
2025-07-24 20:57:19 -04:00
|
|
|
const result = await sql`
|
2025-07-28 12:18:58 -04:00
|
|
|
INSERT INTO users (
|
|
|
|
|
email,
|
|
|
|
|
password,
|
|
|
|
|
first_name,
|
|
|
|
|
last_name,
|
|
|
|
|
username,
|
|
|
|
|
profile_image_url,
|
|
|
|
|
role
|
|
|
|
|
)
|
|
|
|
|
VALUES (
|
|
|
|
|
${email},
|
|
|
|
|
${hashedPassword},
|
|
|
|
|
${firstName},
|
|
|
|
|
${lastName},
|
|
|
|
|
${username},
|
|
|
|
|
${profileImage || null},
|
|
|
|
|
${'user'}
|
|
|
|
|
)
|
|
|
|
|
RETURNING id, email, first_name, last_name, username, profile_image_url, 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"
|
2026-05-29 11:01:03 -04:00
|
|
|
const uniqueSlug = await generateUniqueSlug(SYSTEM_COLLECTION_DB_NAME, existingSlugs);
|
2025-07-27 16:17:51 -04:00
|
|
|
|
|
|
|
|
// 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 (
|
2026-05-29 11:01:03 -04:00
|
|
|
${SYSTEM_COLLECTION_DB_NAME},
|
|
|
|
|
${VOCAB.SYSTEM_COLLECTION_SEED_DESCRIPTION},
|
2025-07-27 16:17:51 -04:00
|
|
|
'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
|
2026-05-23 10:47:11 -04:00
|
|
|
const token = generateToken({ id: user.id, email: user.email, role: user.role });
|
2025-07-23 10:38:16 -04:00
|
|
|
|
2025-07-28 12:18:58 -04:00
|
|
|
// Return user data without password
|
|
|
|
|
const userResponse = {
|
|
|
|
|
id: user.id,
|
|
|
|
|
email: user.email,
|
|
|
|
|
firstName: user.first_name,
|
|
|
|
|
lastName: user.last_name,
|
|
|
|
|
username: user.username,
|
|
|
|
|
profileImage: user.profile_image_url,
|
|
|
|
|
role: user.role,
|
|
|
|
|
createdAt: user.created_at
|
|
|
|
|
};
|
|
|
|
|
|
2025-07-23 10:38:16 -04:00
|
|
|
res.status(201).json({
|
|
|
|
|
success: true,
|
2025-07-28 12:18:58 -04:00
|
|
|
user: userResponse,
|
2025-07-23 10:38:16 -04:00
|
|
|
token
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error('Registration error:', error);
|
|
|
|
|
res.status(500).json({ error: 'Internal server error' });
|
|
|
|
|
}
|
|
|
|
|
}
|