31 lines
942 B
TypeScript
31 lines
942 B
TypeScript
import { NextRequest, NextResponse } from 'next/server';
|
|
import { auth } from '@/auth';
|
|
import { cancelSubscription } from '@/lib/billing/cancel-subscription';
|
|
|
|
export const dynamic = 'force-dynamic';
|
|
|
|
export async function POST(req: NextRequest) {
|
|
const session = await auth();
|
|
if (!session?.user?.id) {
|
|
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
|
|
}
|
|
|
|
const userId = session.user.id;
|
|
|
|
try {
|
|
const result = await cancelSubscription(userId);
|
|
if (!result.success) {
|
|
const isNotFound = result.error === 'No active subscription found';
|
|
return NextResponse.json(
|
|
{ error: result.error },
|
|
{ status: isNotFound ? 404 : 400 }
|
|
);
|
|
}
|
|
|
|
return NextResponse.json({ success: true });
|
|
} catch (error) {
|
|
console.error('[billing/cancel] Route handler crash:', error);
|
|
return NextResponse.json({ error: 'Failed to cancel subscription' }, { status: 500 });
|
|
}
|
|
}
|