import { appUrl } from "@/lib/base-path"; import { externalRedirect } from "@/lib/http"; import { NextResponse } from "next/server"; import { authorizeUrl, createSocialState, enabledProviders, parseProvider, safeRedirectPath, SOCIAL_STATE_COOKIE, SOCIAL_STATE_PATH, SOCIAL_STATE_TTL_SECONDS, } from "@/lib/auth/social"; export const runtime = "nodejs"; export const dynamic = "force-dynamic"; /** Public: sends the browser to the Google / Facebook sign-in dialog. */ export async function GET(req: Request, ctx: RouteContext<"/api/auth/oauth/[provider]/start">) { const { provider: raw } = await ctx.params; const url = new URL(req.url); const provider = parseProvider(raw); if (!provider || !enabledProviders().includes(provider)) { return NextResponse.redirect(`${appUrl(url.origin)}/login?error=not_configured`); } const { nonce, cookie } = await createSocialState(provider, { from: safeRedirectPath(url.searchParams.get("from")), remember: url.searchParams.get("remember") !== "0", }); const res = externalRedirect(authorizeUrl(provider, url.origin, nonce)); res.cookies.set(SOCIAL_STATE_COOKIE, cookie, { httpOnly: true, secure: process.env.NODE_ENV === "production", // Lax: must survive the top-level redirect back from the provider. sameSite: "lax", path: SOCIAL_STATE_PATH, maxAge: SOCIAL_STATE_TTL_SECONDS, }); return res; }