fix: restore session before auth middleware in SPA mode#571
fix: restore session before auth middleware in SPA mode#571zlotnika wants to merge 1 commit intonuxt-modules:mainfrom
Conversation
|
@zlotnika is attempting to deploy a commit to the NuxtLabs Team on Vercel. A member of the Team first needs to authorize it. |
| // 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) { |
There was a problem hiding this comment.
Not sure if this is the best value to check.
commit: |
|
Thanks @zlotnika, can you please remove the |
|
@larbish @zlotnika Hello I’m linking this issue for additional context about the same problem: The session is not yet initialized in the plugin when the middleware runs its check. The session only gets set afterward, inside the page:start hook, which executes after the middleware. As a result, the middleware runs before the session is available, leading to the issue described above. |
|
Alright, sorry for forgetting this for so long. @larbish I've removed dist. Let me know if you want anything else! |
be268ed to
d6fe4d5
Compare
In SPA mode (useSsrCookies: false), explicitly call getSession() during plugin setup so the session state is populated before auth middleware runs. This prevents authenticated users from being incorrectly redirected on direct navigation or page reload. Fixes nuxt-modules#496 Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
d6fe4d5 to
a0c41ee
Compare
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