mirror of https://github.com/buster-so/buster.git
34 lines
1.3 KiB
TypeScript
34 lines
1.3 KiB
TypeScript
import { isDev } from '@/config/dev';
|
|
import { createClient } from '@/lib/supabase/server';
|
|
import { NextResponse } from 'next/server';
|
|
|
|
export async function GET(request: Request) {
|
|
const { searchParams, origin } = new URL(request.url);
|
|
const code = searchParams.get('code_challenge') || searchParams.get('code'); //we were not seeing code, just code_challenge?
|
|
// if "next" is in param, use it as the redirect URL
|
|
const next = searchParams.get('next') ?? '/';
|
|
|
|
if (code) {
|
|
const supabase = await createClient();
|
|
const { error } = await supabase.auth.exchangeCodeForSession(code);
|
|
|
|
if (!error) {
|
|
const forwardedHost = request.headers.get('x-forwarded-host'); // original origin before load balancer
|
|
|
|
if (isDev) {
|
|
// we can be sure that there is no load balancer in between, so no need to watch for X-Forwarded-Host
|
|
return NextResponse.redirect(`${origin}/app`);
|
|
} else if (forwardedHost) {
|
|
return NextResponse.redirect(`https://${forwardedHost}${next}`);
|
|
} else {
|
|
return NextResponse.redirect(`${origin}/app`);
|
|
}
|
|
}
|
|
|
|
console.error('ERROR EXCHANGING CODE FOR SESSION', { error });
|
|
}
|
|
|
|
// return the user to an error page with instructions
|
|
return NextResponse.redirect(`${origin}/auth/auth-code-error`);
|
|
}
|