diff --git a/packages/auth/UserContextProvider.ts b/packages/auth/UserContextProvider.ts index 1e8acc5..62e8c35 100644 --- a/packages/auth/UserContextProvider.ts +++ b/packages/auth/UserContextProvider.ts @@ -28,7 +28,7 @@ export default defineComponent({ ) return () => { - console.log('authListener') + // console.log('authListener') authListener?.subscription.unsubscribe() } }) @@ -45,8 +45,7 @@ export default defineComponent({ export const useSupabaseUser = (supabaseClient: SupabaseClient) => { const supabaseUser = ref(null) - // Asyncronous refresh session and ensure user is still logged in - supabaseClient?.auth.getSession().then(({ data: { session } }) => { + const setSupbaseUser = (session: Session) => { if (session) { if (JSON.stringify(supabaseUser.value) !== JSON.stringify(session.user)) { supabaseUser.value = session.user @@ -54,6 +53,23 @@ export const useSupabaseUser = (supabaseClient: SupabaseClient) => { } else { supabaseUser.value = null } + } + + // Asyncronous refresh session and ensure user is still logged in + supabaseClient?.auth.getSession().then(({ data: { session } }) => { + session && setSupbaseUser(session) + }) + + onMounted(async () => { + const { data: authListener } = supabaseClient.auth.onAuthStateChange( + async (event, newSession) => { + newSession && setSupbaseUser(newSession) + } + ) + + return () => { + authListener?.subscription.unsubscribe() + } }) return {