2024-12-15 08:23:18 -05:00
|
|
|
import { NextResponse } from 'next/server';
|
2025-01-14 22:47:04 -05:00
|
|
|
import { db } from '@db/index';
|
|
|
|
|
import { usersMerged } from '@schemas/schema';
|
2024-12-15 08:23:18 -05:00
|
|
|
import bcrypt from 'bcryptjs';
|
|
|
|
|
import { eq } from 'drizzle-orm';
|
|
|
|
|
|
|
|
|
|
export async function POST(request: Request) {
|
|
|
|
|
try {
|
|
|
|
|
const { firstName, username, password, email } = await request.json();
|
|
|
|
|
const hashedPassword = await bcrypt.hash(password, 10);
|
|
|
|
|
|
|
|
|
|
const newUser = {
|
|
|
|
|
firstName,
|
|
|
|
|
username,
|
|
|
|
|
email,
|
|
|
|
|
passwordHash: hashedPassword,
|
2025-01-14 22:47:04 -05:00
|
|
|
} satisfies typeof usersMerged.$inferInsert;
|
2024-12-15 08:23:18 -05:00
|
|
|
|
2025-01-14 22:47:04 -05:00
|
|
|
await db.insert(usersMerged).values(newUser);
|
2024-12-15 08:23:18 -05:00
|
|
|
|
|
|
|
|
return NextResponse.json(
|
|
|
|
|
{ message: 'User created successfully', redirect: '/' },
|
|
|
|
|
{ status: 201 }
|
|
|
|
|
);
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error('Signup error:', error);
|
|
|
|
|
return NextResponse.json(
|
|
|
|
|
{ error: 'Failed to create user' },
|
|
|
|
|
{ status: 500 }
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|