ClauseGuard / web /app /auth /callback /route.ts
anky2002's picture
feat: add user authentication checks and improve navigation with logout functionality
ae80634
raw
history blame contribute delete
894 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");
let next = requestUrl.searchParams.get("next") || "/dashboard-pages/dashboard";
const origin = requestUrl.origin;
// Prevent open redirect
if (next && !next.startsWith("/")) {
next = "/dashboard-pages/dashboard";
}
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}`);
}