|
| 1 | +import{NextResponse}from'next/server' |
| 2 | + |
| 3 | +import{createClient}from'@/lib/supabase/server' |
| 4 | + |
| 5 | +exportasyncfunctionGET(request:Request){ |
| 6 | +const{ searchParams, origin}=newURL(request.url) |
| 7 | +constcode=searchParams.get('code') |
| 8 | +letnext=searchParams.get('next')??'/' |
| 9 | +if(!next.startsWith('/')){ |
| 10 | +next='/' |
| 11 | +} |
| 12 | + |
| 13 | +if(code){ |
| 14 | +constsupabase=awaitcreateClient() |
| 15 | +const{ error}=awaitsupabase.auth.exchangeCodeForSession(code) |
| 16 | +if(!error){ |
| 17 | +constforwardedHost=request.headers.get('x-forwarded-host') |
| 18 | +constisLocalEnv=process.env.NODE_ENV==='development' |
| 19 | +if(isLocalEnv){ |
| 20 | +returnNextResponse.redirect(`${origin}${next}`) |
| 21 | +}elseif(forwardedHost){ |
| 22 | +returnNextResponse.redirect(`https://${forwardedHost}${next}`) |
| 23 | +}else{ |
| 24 | +returnNextResponse.redirect(`${origin}${next}`) |
| 25 | +} |
| 26 | +} |
| 27 | +} |
| 28 | + |
| 29 | +returnNextResponse.redirect( |
| 30 | +`${origin}/auth/error?error=An error occurred while signing in.`, |
| 31 | +) |
| 32 | +} |