http://localhost:3000/auth/auth-code-error#access_token=eyJhbGciOiJIUzI1........
action.ts
app/auth/callback/route.ts
'use server'
import { redirect } from "next/navigation";
import { supabase } from "./client";
import { Provider } from "@supabase/supabase-js";
async function signInWith(provider: Provider) {
const auth_callback_url = `${process.env.SITE_URL}/auth/callback`;
const { data, error } = await supabase.auth.signInWithOAuth({
provider,
options: {
redirectTo: auth_callback_url
}
});
if (error) {
console.error("Auth Error:", error);
return;
}
console.log("Redirecting to:", data.url);
redirect(data.url);
}
// Define as a function, not immediate execution
const signInWithGoogle = () => signInWith('google');
export { signInWithGoogle };'use server'
import { redirect } from "next/navigation";
import { supabase } from "./client";
import { Provider } from "@supabase/supabase-js";
async function signInWith(provider: Provider) {
const auth_callback_url = `${process.env.SITE_URL}/auth/callback`;
const { data, error } = await supabase.auth.signInWithOAuth({
provider,
options: {
redirectTo: auth_callback_url
}
});
if (error) {
console.error("Auth Error:", error);
return;
}
console.log("Redirecting to:", data.url);
redirect(data.url);
}
// Define as a function, not immediate execution
const signInWithGoogle = () => signInWith('google');
export { signInWithGoogle };app/auth/callback/route.ts
import { NextResponse } from 'next/server'
// The client you created from the Server-Side Auth instructions
import { supabase } from '@/utils/supabase/client'
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 { error } = await supabase.auth.exchangeCodeForSession(code)
if (!error) {
const forwardedHost = request.headers.get('x-forwarded-host') // original origin before load balancer
const isLocalEnv = process.env.NODE_ENV === 'development'
if (isLocalEnv) {
// 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}${next}`)
} else if (forwardedHost) {
return NextResponse.redirect(`https://${forwardedHost}${next}`)
} else {
return NextResponse.redirect(`${origin}${next}`)
}
}
}
// return the user to an error page with instructions
return NextResponse.redirect(`${origin}/auth/auth-code-error`)
}import { NextResponse } from 'next/server'
// The client you created from the Server-Side Auth instructions
import { supabase } from '@/utils/supabase/client'
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 { error } = await supabase.auth.exchangeCodeForSession(code)
if (!error) {
const forwardedHost = request.headers.get('x-forwarded-host') // original origin before load balancer
const isLocalEnv = process.env.NODE_ENV === 'development'
if (isLocalEnv) {
// 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}${next}`)
} else if (forwardedHost) {
return NextResponse.redirect(`https://${forwardedHost}${next}`)
} else {
return NextResponse.redirect(`${origin}${next}`)
}
}
}
// return the user to an error page with instructions
return NextResponse.redirect(`${origin}/auth/auth-code-error`)
}