Open-source agentic AI Search Execution Platform. Track AI visibility, find opportunities, and let agents turn signals into prioritized actions and drafted fixes across ChatGPT, Claude, Gemini, Google AI Overviews, Google AI Mode, Perplexity, Grok, and Microsoft Copilot.
TypeScript aeoai-agentai-searchanswer-engine-optimizationbrand-monitoringchatgptclaudecopilotdockergeminigeogoogle-ai-overviewsgrokmcpnextjsopen-sourceperplexityseosupabasetypescript
import { NextResponse } from 'next/server';
import { createClient } from '@/lib/supabase/server';
import { stripe } from '@/lib/stripe';
/**
* POST — create a Stripe Customer Portal session and return its URL.
*
* The portal is the only place users can update their payment method,
* billing address and tax IDs (we don't mirror those into our own UI).
* Plan switching and cancellation stay in our app, so the portal
* configuration in the Stripe Dashboard should keep those features off.
*/
export async function POST() {
try {
const supabase = await createClient();
const {
data: { user },
} = await supabase.auth.getUser();
if (!user) {
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
}
const { data: profile } = await supabase
.from('profiles')
.select('organization_id')
.eq('id', user.id)
.single();
if (!profile?.organization_id) {
return NextResponse.json({ error: 'No organization found' }, { status: 400 });
}
const { data: org } = await supabase
.from('organizations')
.select('stripe_customer_id')
.eq('id', profile.organization_id)
.single();
const customerId = org?.stripe_customer_id as string | null;
if (!customerId) {
return NextResponse.json({ error: 'No billing account found' }, { status: 400 });
}
const appUrl = process.env.NEXT_PUBLIC_APP_URL || 'http://localhost:3000';
const session = await stripe.billingPortal.sessions.create({
customer: customerId,
return_url: `${appUrl}/dashboard/settings?tab=billing`,
});
return NextResponse.json({ url: session.url });
} catch (err) {
console.error('[stripe/portal]', err);
return NextResponse.json(
{ error: err instanceof Error ? err.message : 'Internal error' },
{ status: 500 },
);
}
}