SupabaseS
Supabase2y ago
kyle

NextJS auth helpers - invalid flow state

I'm following the auth helpers guide (https://supabase.com/docs/guides/auth/auth-helpers/nextjs) to set up email signups, but I'm getting AuthApiError: invalid flow state, flow state has expired when the user confirms via email link and hits the redirect URL. This only seems to happen if the user wait ~5 minutes to confirm via email; confirming immediately after receiving the email seems to work. Is there any way to increase the expiration time?

Here's my signup code:
    await supabase.auth
      .signUp({
        email,
        password,
        options: {
          emailRedirectTo: `${location.origin}/api/auth/callback`,
          data: {
            handle,
          },
        },
      })


And here's my redirect route:
export async function GET(request: NextRequest) {
  const requestUrl = new URL(request.url)
  const code = requestUrl.searchParams.get('code')

  if (code) {
    const cookieStore = cookies()
    const supabase = createRouteHandlerClient({
      cookies: () => cookieStore,
    })
    await supabase.auth.exchangeCodeForSession(code)
  }

  // URL to redirect to after sign in process completes
  return NextResponse.redirect(requestUrl.origin)
}
Authentication and Authorization helpers for creating an authenticated Supabase client with the Next.js 13 App Router.
Was this page helpful?