© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
SupabaseS
Supabase•12mo ago•
23 replies
3Froto

http://localhost:3000/auth/auth-code-error#access_token=eyJhbGciOiJIUzI1........

action.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`)
}
Supabase banner
SupabaseJoin
Supabase gives you the tools, documentation, and community that makes managing databases, authentication, and backend infrastructure a lot less overwhelming.
45,816Members
Resources
Was this page helpful?

Similar Threads

Recent Announcements

Similar Threads

Localhost:3000
SupabaseSSupabase / help-and-questions
3y ago
Auth token error
SupabaseSSupabase / help-and-questions
13mo ago
sb-access-token,sb-refresh-token VS supabase-auth-token?
SupabaseSSupabase / help-and-questions
4y ago
Getting the auth provider access token
SupabaseSSupabase / help-and-questions
4y ago