© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
SupabaseS
Supabase•3y ago•
2 replies
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
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,
          },
        },
      })
    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)
}
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)
}
Supabase Auth with the Next.js App Router | Supabase Docs
Authentication and Authorization helpers for creating an authenticated Supabase client with the Next.js 13 App Router.
Supabase Auth with the Next.js App Router | Supabase Docs
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

nextjs auth wihtout helpers
SupabaseSSupabase / help-and-questions
4y ago
Struggles auth-helpers-nextjs
SupabaseSSupabase / help-and-questions
4y ago
Auth helpers
SupabaseSSupabase / help-and-questions
3y ago
Nextjs auth-helpers with middleware and magiclinks
SupabaseSSupabase / help-and-questions
4y ago