import { NextRequest, NextResponse } from 'next/server'; import { getServerSession } from 'next-auth'; import { authOptions } from '@/pages/api/auth/[...nextauth]'; import { subscribeToPush } from '@/lib/push-notifications'; export async function POST(request: NextRequest) { try { const session = await getServerSession(authOptions); if (!session?.user?.id) { return NextResponse.json({ error: 'Unauthorized' }, { status: 401 }); } const subscription = await request.json(); if (!subscription.endpoint || !subscription.keys) { return NextResponse.json( { error: 'Invalid subscription object' }, { status: 400 } ); } await subscribeToPush(session.user.id, subscription); return NextResponse.json({ success: true }); } catch (error) { console.error('Failed to subscribe to push:', error); return NextResponse.json( { error: 'Failed to subscribe to push notifications' }, { status: 500 } ); } }