Overview
Read redirect_to and optional variant_id on the auth callback; prefer a safe internal path and hand off to Polar checkout when a product ID is present.
import { redirect } from 'next/navigation'
import { DashboardRoute } from '@/constants/routes'
import { startCheckoutAction } from '@/actions/startCheckoutAction'
export async function handlePostAuth(params: {
redirect_to?: string | null
variant_id?: string | null
}) {
const { redirect_to, variant_id } = params
if (variant_id) {
const res = await startCheckoutAction({ variantId: variant_id })
if (res?.success && res.data?.url) return redirect(res.data.url)
}
const safe = redirect_to?.startsWith('/')
? redirect_to
: DashboardRoute.OverviewPage
return redirect(safe)
}Key conventions
- Only redirect to
redirect_toif it starts with/— this prevents open redirect attacks to external URLs. - A
variant_idmeans the user clicked Subscribe before logging in. Create the Polar checkout session server-side and redirect; do not send the raw product ID to the client. - Fall back to
DashboardRoute.OverviewPagewhen there is no valid redirect target — never leave a blank redirect. startCheckoutActionis a server action that returnsActionResponsewithdata.urlpointing to the Polar-hosted checkout page.