diff options
Diffstat (limited to 'src/app')
| -rw-r--r-- | src/app/api/auth/register/route.ts | 27 |
1 files changed, 10 insertions, 17 deletions
diff --git a/src/app/api/auth/register/route.ts b/src/app/api/auth/register/route.ts index 3276756..ec109b7 100644 --- a/src/app/api/auth/register/route.ts +++ b/src/app/api/auth/register/route.ts @@ -4,17 +4,19 @@ import dbConnect from "@/lib/mongodb" import User from "@/model/User" import { registerSchema, formatZodError } from "@/lib/validation" +// Define interface for MongoDB duplicate key error +interface MongoError extends Error { + code?: number; +} + export async function POST(request: NextRequest) { try { const body = await request.json() - const result = registerSchema.safeParse(body) if (!result.success) { - const errors = formatZodError(result.error) - return NextResponse.json( - { error: "Validation failed", details: errors }, + { error: "Validation failed", details: formatZodError(result.error) }, { status: 400 } ) } @@ -23,14 +25,6 @@ export async function POST(request: NextRequest) { await dbConnect() - const existingUser = await User.findOne({ email }) - if (existingUser) { - return NextResponse.json( - { error: "User already exists" }, - { status: 409 } - ) - } - const hashedPassword = await bcrypt.hash(password, 12) const user = await User.create({ @@ -44,18 +38,17 @@ export async function POST(request: NextRequest) { { status: 201 } ) } catch (error) { - console.error("Registration error:", error) - - if (error && typeof error === 'object' && 'code' in error && error.code === 11000) { + const mongoError = error as MongoError; + if (mongoError.code === 11000) { return NextResponse.json( { error: "User already exists" }, { status: 409 } ) } - + console.error("Registration error:", error) return NextResponse.json( { error: "Internal server error" }, { status: 500 } ) } -}
\ No newline at end of file +} |
