© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
SupabaseS
Supabase•2mo ago
bighead

Issue with onAuthStateChange in Pinia + Supabase

PostgRESTauth
I have a Pinia store with
updateSettings
updateSettings
function to update user settings in Supabase.

I use
supabase.auth.onAuthStateChange
supabase.auth.onAuthStateChange
to initialize the store (currentTopics, currentLevel, etc.) when a user signs in.

On page reload,
onAuthStateChange
onAuthStateChange
fires immediately with the existing session, and I see
console.log('Updated')
console.log('Updated')
to appear even though the user hasn’t actually signed in again (the token already exists).

If I try to call another async function
updateSettings
updateSettings
after that, it sometimes fails because
supabase.auth.getUser()
supabase.auth.getUser()
in there may return null or undefined temporarily, depending on how the session is restored.
supabase.auth.onAuthStateChange((event, session) => {
    setTimeout(async () => {
        if (event === 'SIGNED_IN') {
            const { data, error } = await supabase
                .from('study_settings')
                .select<'*', StudySettings>('*')
                .eq('user_id', session.user.id)
                .single();

            if (error) return;

            if (data) {
                currentLevel.value = levels.value.indexOf(data.level);
            }
            console.log('Updated', levels.value[currentLevel.value]);
        }
    }, 0);
});
supabase.auth.onAuthStateChange((event, session) => {
    setTimeout(async () => {
        if (event === 'SIGNED_IN') {
            const { data, error } = await supabase
                .from('study_settings')
                .select<'*', StudySettings>('*')
                .eq('user_id', session.user.id)
                .single();

            if (error) return;

            if (data) {
                currentLevel.value = levels.value.indexOf(data.level);
            }
            console.log('Updated', levels.value[currentLevel.value]);
        }
    }, 0);
});

Any solution? Where should I keep
onAuthStateChange
onAuthStateChange
?
Supabase banner
SupabaseJoin
Supabase gives you the tools, documentation, and community that makes managing databases, authentication, and backend infrastructure a lot less overwhelming.
45,816Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

onAuthStateChange not triggered when using server-side signIn/signOut in Next.js with Supabase
SupabaseSSupabase / help-and-questions
7mo ago
Email Confirmation - onAuthStateChange?
SupabaseSSupabase / help-and-questions
4y ago
onAuthStateChange never firing.
SupabaseSSupabase / help-and-questions
4y ago
Resolving 'deleteUser' Property Issue with SupabaseAuthClient in React
SupabaseSSupabase / help-and-questions
3y ago