39 lines
1.4 KiB
TypeScript
39 lines
1.4 KiB
TypeScript
import { NextResponse } from 'next/server'
|
|
import { createServerClient, type CookieOptions } from '@supabase/ssr'
|
|
import { cookies } from 'next/headers'
|
|
|
|
export async function GET(request: Request) {
|
|
const { searchParams, origin } = new URL(request.url)
|
|
const code = searchParams.get('code')
|
|
// if "next" is in param, use it as the redirect URL
|
|
const next = searchParams.get('next') ?? '/'
|
|
|
|
if (code) {
|
|
const cookieStore = await cookies()
|
|
const supabase = createServerClient(
|
|
process.env.NEXT_PUBLIC_SUPABASE_URL!,
|
|
process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY!,
|
|
{
|
|
cookies: {
|
|
get(name: string) {
|
|
return cookieStore.get(name)?.value
|
|
},
|
|
set(name: string, value: string, options: CookieOptions) {
|
|
cookieStore.set({ name, value, ...options })
|
|
},
|
|
remove(name: string, options: CookieOptions) {
|
|
cookieStore.delete({ name, ...options })
|
|
},
|
|
},
|
|
}
|
|
)
|
|
const { error } = await supabase.auth.exchangeCodeForSession(code)
|
|
if (!error) {
|
|
return NextResponse.redirect(`${origin}${next}`)
|
|
}
|
|
}
|
|
|
|
// return the user to an error page with instructions
|
|
return NextResponse.redirect(`${origin}/auth/auth-code-error`)
|
|
}
|