ClauseGuard / web /app /auth /callback /route.ts
gaurv007's picture
Fix all auth edge cases: proxy blocks+redirects properly, auth pages redirect if logged in, loading state prevents form flash, callback honors ?next param
f36152d verified
raw
history blame
784 Bytes
import { createClient } from "@/lib/supabase/server";
import { NextResponse } from "next/server";
export async function GET(request: Request) {
const requestUrl = new URL(request.url);
const code = requestUrl.searchParams.get("code");
const next = requestUrl.searchParams.get("next") || "/dashboard-pages/dashboard";
const origin = requestUrl.origin;
if (code) {
const supabase = await createClient();
const { error } = await supabase.auth.exchangeCodeForSession(code);
if (!error) {
return NextResponse.redirect(`${origin}${next}`);
}
}
// If code exchange failed, try hash-based recovery (password reset flow)
// Supabase sends recovery tokens as hash fragments which the client handles
return NextResponse.redirect(`${origin}${next}`);
}