120 lines
2.9 KiB
TypeScript
120 lines
2.9 KiB
TypeScript
import { NextRequest, NextResponse } from 'next/server'
|
|
import { z } from 'zod'
|
|
|
|
const ForgotPasswordSchema = z.object({
|
|
email: z.string().email('Please enter a valid email address'),
|
|
})
|
|
|
|
export async function POST(request: NextRequest) {
|
|
try {
|
|
const body = await request.json()
|
|
|
|
// Validate the request body
|
|
const validatedData = ForgotPasswordSchema.parse(body)
|
|
|
|
// Simulate processing delay
|
|
await new Promise(resolve => setTimeout(resolve, 1000))
|
|
|
|
// Dummy API - Always return an error for demonstration
|
|
// You can change this behavior for testing different scenarios
|
|
|
|
const email = validatedData.email
|
|
|
|
// Simulate different error scenarios based on email
|
|
if (email === 'test@example.com') {
|
|
return NextResponse.json(
|
|
{
|
|
success: false,
|
|
error: {
|
|
message: 'Email address not found in our system',
|
|
code: 'EMAIL_NOT_FOUND'
|
|
}
|
|
},
|
|
{ status: 404 }
|
|
)
|
|
}
|
|
|
|
if (email.includes('blocked')) {
|
|
return NextResponse.json(
|
|
{
|
|
success: false,
|
|
error: {
|
|
message: 'This email address has been temporarily blocked',
|
|
code: 'EMAIL_BLOCKED'
|
|
}
|
|
},
|
|
{ status: 429 }
|
|
)
|
|
}
|
|
|
|
if (email.includes('invalid')) {
|
|
return NextResponse.json(
|
|
{
|
|
success: false,
|
|
error: {
|
|
message: 'Invalid email format',
|
|
code: 'INVALID_EMAIL'
|
|
}
|
|
},
|
|
{ status: 400 }
|
|
)
|
|
}
|
|
|
|
// Default error response (500 Internal Server Error)
|
|
return NextResponse.json(
|
|
{
|
|
success: false,
|
|
error: {
|
|
message: 'Unable to process password reset request at this time. Please try again later.',
|
|
code: 'SERVER_ERROR'
|
|
}
|
|
},
|
|
{ status: 500 }
|
|
)
|
|
|
|
// Uncomment below for success response (when you want to test success state)
|
|
/*
|
|
return NextResponse.json(
|
|
{
|
|
success: true,
|
|
message: 'Password reset email sent successfully',
|
|
data: {
|
|
email: validatedData.email,
|
|
resetTokenExpiry: Date.now() + 3600000 // 1 hour from now
|
|
}
|
|
},
|
|
{ status: 200 }
|
|
)
|
|
*/
|
|
|
|
} catch (error) {
|
|
console.error('Forgot password API error:', error)
|
|
|
|
// Handle validation errors
|
|
if (error instanceof z.ZodError) {
|
|
return NextResponse.json(
|
|
{
|
|
success: false,
|
|
error: {
|
|
message: 'Invalid request data',
|
|
code: 'VALIDATION_ERROR',
|
|
details: error.issues
|
|
}
|
|
},
|
|
{ status: 400 }
|
|
)
|
|
}
|
|
|
|
// Handle other errors
|
|
return NextResponse.json(
|
|
{
|
|
success: false,
|
|
error: {
|
|
message: 'An unexpected error occurred',
|
|
code: 'INTERNAL_ERROR'
|
|
}
|
|
},
|
|
{ status: 500 }
|
|
)
|
|
}
|
|
} |