import { NextResponse } from 'next/server'; import { getServerSession } from 'next-auth'; import { authOptions } from '@/lib/auth'; import { PrismaClient } from '@prisma/client'; import { submitSecurityCode } from '@/lib/apple-reminders'; const prisma = new PrismaClient(); export async function POST(req: Request) { try { const session = await getServerSession(authOptions); if (!session?.user?.email) { return NextResponse.json({ error: 'Unauthorized' }, { status: 401 }); } const body = await req.json(); const { email, code, password } = body; if (!email || !code || !password) { return NextResponse.json( { error: 'Email, security code, and password are required' }, { status: 400 } ); } console.log('[REMINDERS VERIFY] Submitting 2FA code for:', email); const result = await submitSecurityCode(email, code, password); if (!result.success) { return NextResponse.json( { error: result.error || 'Invalid security code' }, { status: 400 } ); } console.log('[REMINDERS VERIFY] 2FA verification successful'); const user = await prisma.user.findUnique({ where: { email: session.user.email } }); if (!user) { return NextResponse.json({ error: 'User not found' }, { status: 404 }); } // Save connection as apple-reminders provider const accessToken = `${email}:${password}`; const existingConnection = await prisma.calendarConnection.findFirst({ where: { userId: user.id, provider: 'apple-reminders' } }); if (existingConnection) { await prisma.calendarConnection.update({ where: { id: existingConnection.id }, data: { accessToken, updatedAt: new Date() } }); } else { await prisma.calendarConnection.create({ data: { userId: user.id, provider: 'apple-reminders', accessToken, calendars: [] } }); } return NextResponse.json({ success: true, message: 'Apple Reminders verified and connected successfully!' }); } catch (error: any) { console.error('[REMINDERS VERIFY] Error:', error); return NextResponse.json( { error: error.message || 'Verification failed' }, { status: 500 } ); } }