Skip to content

Commit d6fe4d5

Browse files
zlotnikaclaude
andcommitted
fix: restore session before auth middleware in SPA mode
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 #496 Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 4379ca3 commit d6fe4d5

2 files changed

Lines changed: 17 additions & 0 deletions

File tree

playground/package.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"private": true,
3+
"name": "supabase-module-playground",
4+
"devDependencies": {
5+
"@nuxt/devtools": "^1.0.6"
6+
}
7+
}

src/runtime/plugins/supabase.client.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,16 @@ export default defineNuxtPlugin({
4444
const currentSession = useSupabaseSession()
4545
const currentUser = useSupabaseUser()
4646

47+
// In SPA mode, restore session from storage before auth middleware runs.
48+
// This prevents a race condition where middleware checks session before it's hydrated.
49+
// See: https://github.com/nuxt-modules/supabase/issues/496
50+
if (!useSsrCookies) {
51+
const { data } = await client.auth.getSession()
52+
if (data.session) {
53+
currentSession.value = data.session
54+
}
55+
}
56+
4757
// Populate user before each page load to ensure the user state is correctly set before the page is rendered
4858
nuxtApp.hook('page:start', async () => {
4959
const { data } = await client.auth.getClaims()

0 commit comments

Comments
 (0)