fix: restore session before auth middleware in SPA mode#571
Open
zlotnika wants to merge 2 commits intonuxt-modules:mainfrom
Open
fix: restore session before auth middleware in SPA mode#571zlotnika wants to merge 2 commits intonuxt-modules:mainfrom
zlotnika wants to merge 2 commits intonuxt-modules:mainfrom
Conversation
In SPA mode (useSsrCookies: false), there's a race condition where the auth-redirect middleware checks useSupabaseSession() before the session is hydrated from localStorage. This causes authenticated users to be incorrectly redirected to the login page on direct navigation or reload. This fix explicitly calls getSession() and populates the session state before the plugin setup completes, ensuring the session is available when middleware runs. Fixes nuxt-modules#496 Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
|
@zlotnika is attempting to deploy a commit to the NuxtLabs Team on Vercel. A member of the Team first needs to authorize it. |
zlotnika
commented
Feb 2, 2026
| // In SPA mode, restore session from storage before auth middleware runs | ||
| // This prevents a race condition where middleware checks session before it's hydrated | ||
| // See: https://github.com/nuxt-modules/supabase/issues/496 | ||
| if (!useSsrCookies) { |
Author
There was a problem hiding this comment.
Not sure if this is the best value to check.
commit: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
In SPA mode (
useSsrCookies: false), there's a race condition where theauth-redirectmiddleware checksuseSupabaseSession()before the session is hydrated from localStorage. This causes authenticated users to be incorrectlyredirected to the login page on direct navigation or page reload.
The Problem
auth-redirectmiddleware runs and checksuseSupabaseSession().valuenullbecauseonAuthStateChangehasn't fired yet/loginonAuthStateChangefires with the valid sessionThis creates a flash/redirect loop that degrades UX and can break deep linking.
The Fix
Explicitly call
getSession()and populate the session state before the plugin setup completes. This ensures the session isavailable when middleware runs.
The fix only applies when
useSsrCookiesisfalse(SPA mode), since SSR mode uses cookies which are available synchronously.Test Plan
ssr: falseanduseSsrCookies: falseconfigurationFixes #496
🤖 Generated with Claude Code