Skip to content

Commit 06674fd

Browse files
authored
fix(session): restore session before auth middleware in SPA mode (#571)
1 parent f46a1c8 commit 06674fd

1 file changed

Lines changed: 10 additions & 0 deletions

File tree

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)