2026-03-13 02:23:01 +08:00
|
|
|
import { NextRequest, NextResponse } from "next/server";
|
|
|
|
|
import { createSupabaseRouteClient, hasSupabaseServerEnv } from "@/lib/supabase/server";
|
2026-05-24 18:33:47 +08:00
|
|
|
import { getConfiguredSiteUrl } from "@/lib/site-url";
|
2026-03-13 02:23:01 +08:00
|
|
|
|
|
|
|
|
function normalizeNextPath(input: string | null) {
|
|
|
|
|
const fallback = "/";
|
|
|
|
|
const raw = String(input || "").trim();
|
|
|
|
|
if (!raw) return fallback;
|
|
|
|
|
if (!raw.startsWith("/")) return fallback;
|
|
|
|
|
if (raw.startsWith("//")) return fallback;
|
|
|
|
|
return raw;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function GET(request: NextRequest) {
|
2026-05-26 04:27:37 +08:00
|
|
|
const siteUrl = getConfiguredSiteUrl();
|
|
|
|
|
if (siteUrl) {
|
|
|
|
|
const expectedHost = new URL(siteUrl).host;
|
|
|
|
|
const requestHost =
|
|
|
|
|
request.headers.get("x-forwarded-host") ||
|
|
|
|
|
request.headers.get("host") ||
|
|
|
|
|
"";
|
|
|
|
|
if (requestHost !== expectedHost) {
|
|
|
|
|
const canonicalCallbackUrl = new URL(request.nextUrl.pathname, siteUrl);
|
2026-05-24 18:33:47 +08:00
|
|
|
canonicalCallbackUrl.search = request.nextUrl.search;
|
|
|
|
|
return NextResponse.redirect(canonicalCallbackUrl);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-13 02:23:01 +08:00
|
|
|
const nextPath = normalizeNextPath(request.nextUrl.searchParams.get("next"));
|
2026-05-26 04:48:07 +08:00
|
|
|
const baseUrl = siteUrl || request.nextUrl.origin;
|
|
|
|
|
const redirectUrl = new URL(nextPath, baseUrl);
|
2026-03-13 02:23:01 +08:00
|
|
|
|
|
|
|
|
if (!hasSupabaseServerEnv()) {
|
|
|
|
|
return NextResponse.redirect(redirectUrl);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const response = NextResponse.redirect(redirectUrl);
|
|
|
|
|
const supabase = createSupabaseRouteClient(request, response);
|
|
|
|
|
const code = request.nextUrl.searchParams.get("code");
|
|
|
|
|
if (code) {
|
|
|
|
|
await supabase.auth.exchangeCodeForSession(code);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return response;
|
|
|
|
|
}
|