70 lines
2.0 KiB
TypeScript
70 lines
2.0 KiB
TypeScript
import { NextRequest, NextResponse } from 'next/server'
|
|
import { prisma } from '@/lib/prisma'
|
|
import { generateEmailToken } from '@/lib/auth'
|
|
|
|
export async function POST(request: NextRequest) {
|
|
try {
|
|
const body = await request.json()
|
|
const { email } = body
|
|
|
|
if (!email) {
|
|
return NextResponse.json(
|
|
{ error: 'Email is required' },
|
|
{ status: 400 }
|
|
)
|
|
}
|
|
|
|
// Check if user exists
|
|
const user = await prisma.user.findUnique({
|
|
where: { email },
|
|
select: { id: true, emailVerified: true, displayName: true },
|
|
})
|
|
|
|
if (!user) {
|
|
// Don't reveal that email doesn't exist
|
|
return NextResponse.json(
|
|
{ message: 'If the email exists, a verification link has been sent' },
|
|
{ status: 200 }
|
|
)
|
|
}
|
|
|
|
if (user.emailVerified) {
|
|
return NextResponse.json(
|
|
{ error: 'Email is already verified' },
|
|
{ status: 400 }
|
|
)
|
|
}
|
|
|
|
// Generate verification token
|
|
const emailToken = generateEmailToken()
|
|
const expiresAt = new Date()
|
|
expiresAt.setHours(expiresAt.getHours() + 24) // 24 hours
|
|
|
|
// Update user with verification token
|
|
await prisma.user.update({
|
|
where: { id: user.id },
|
|
data: {
|
|
emailToken,
|
|
resetExpires: expiresAt, // Using resetExpires field for email token expiry
|
|
},
|
|
})
|
|
|
|
// TODO: Send actual email with verification link
|
|
// For now, just log the token (in production, use a proper email service)
|
|
console.log(`Email verification for ${email}: ${emailToken}`)
|
|
|
|
// The verification URL would be:
|
|
// `${process.env.NEXT_PUBLIC_APP_URL}/auth/confirm-email?token=${emailToken}`
|
|
|
|
return NextResponse.json(
|
|
{ message: 'If the email exists, a verification link has been sent' },
|
|
{ status: 200 }
|
|
)
|
|
} catch (error) {
|
|
console.error('Send verification email error:', error)
|
|
return NextResponse.json(
|
|
{ error: 'Failed to send verification email' },
|
|
{ status: 500 }
|
|
)
|
|
}
|
|
} |