Skip to content

Commit

Permalink
fix(client): prevent useCookie usage outside of effect scope (#309)
Browse files Browse the repository at this point in the history
  • Loading branch information
AlbertBrand authored Nov 17, 2023
1 parent 68cb686 commit bb55bed
Showing 1 changed file with 13 additions and 8 deletions.
21 changes: 13 additions & 8 deletions src/runtime/plugins/supabase.client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ export default defineNuxtPlugin({

const supabaseClient = createClient(url, key, clientOptions)

const accessToken = useCookie(`${cookieName}-access-token`, cookieOptions);
const refreshToken = useCookie(`${cookieName}-refresh-token`, cookieOptions);
const providerToken = useCookie(`${cookieName}-provider-token`, cookieOptions);
const providerRefreshToken = useCookie(`${cookieName}-provider-refresh-token`, cookieOptions);

// Handle auth event client side
supabaseClient.auth.onAuthStateChange(async (event, session) => {
// Update user based on received session
Expand All @@ -25,16 +30,16 @@ export default defineNuxtPlugin({

// Use cookies to share session state between server and client
if (event === 'SIGNED_IN' || event === 'TOKEN_REFRESHED') {
useCookie(`${cookieName}-access-token`, cookieOptions).value = session?.access_token
useCookie(`${cookieName}-refresh-token`, cookieOptions).value = session?.refresh_token
if (session.provider_token) useCookie(`${cookieName}-provider-token`, cookieOptions).value = session.provider_token
if (session.provider_refresh_token) useCookie(`${cookieName}-provider-refresh-token`, cookieOptions).value = session.provider_refresh_token
accessToken.value = session?.access_token
refreshToken.value = session?.refresh_token
if (session.provider_token) providerToken.value = session.provider_token
if (session.provider_refresh_token) providerRefreshToken.value = session.provider_refresh_token
}
if (event === 'SIGNED_OUT') {
useCookie(`${cookieName}-access-token`, cookieOptions).value = null
useCookie(`${cookieName}-refresh-token`, cookieOptions).value = null
useCookie(`${cookieName}-provider-token`, cookieOptions).value = null
useCookie(`${cookieName}-provider-refresh-token`, cookieOptions).value = null
accessToken.value = null
refreshToken.value = null
providerToken.value = null
providerRefreshToken.value = null
}
})

Expand Down

0 comments on commit bb55bed

Please sign in to comment.