0
0
Supabasecloud~10 mins

Auth state change listeners in Supabase - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - Auth state change listeners
Start app
Register auth listener
User signs in/out
Listener detects change
Update app state
Reflect changes in UI
The app starts and sets up a listener for authentication changes. When the user signs in or out, the listener detects it and updates the app state and UI accordingly.
Execution Sample
Supabase
const { data: authListener } = supabase.auth.onAuthStateChange((event, session) => {
  console.log('Auth event:', event);
  console.log('Session:', session);
});
This code sets up a listener that runs a function whenever the authentication state changes, logging the event and session info.
Process Table
StepEvent TriggeredListener ActionSession StateApp State Update
1App startsListener runs callback (initial)nullNo user logged in
2User signs inListener runs callback{ user: 'user1' }App updates to logged in
3User signs outListener runs callbacknullApp updates to logged out
4User session refreshListener runs callback{ user: 'user1' }App keeps logged in state
💡 No more auth events; listener waits for next change
Status Tracker
VariableStartAfter Step 2After Step 3After Step 4
eventSIGNED_OUTSIGNED_INSIGNED_OUTTOKEN_REFRESHED
sessionnull{ user: 'user1' }null{ user: 'user1' }
appStatelogged outlogged inlogged outlogged in
Key Moments - 3 Insights
Why does the listener run when the app starts even if the user is not signed in?
The listener callback runs immediately upon registration (step 1) with the initial auth state: event='SIGNED_OUT', session=null, updating app state to logged out.
What happens if the user signs out and then signs in quickly?
The listener runs twice, once for SIGNED_OUT and once for SIGNED_IN events, updating the session and app state accordingly as shown in steps 3 and 2.
Why is the session sometimes null and sometimes an object?
Session is null when no user is signed in (steps 1 and 3), and contains user info when signed in or refreshed (steps 2 and 4).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 2, what is the appState?
Alogged in
Blogged out
Cnull
Dsession expired
💡 Hint
Check the 'App State Update' column at step 2 in the execution_table
At which step does the listener detect the user signing out?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Look at the 'Event Triggered' column for SIGNED_OUT event in the execution_table
If the session is null, what is the likely appState?
Alogged in
Blogged out
Crefreshing
Derror
💡 Hint
Refer to the variable_tracker for session and appState values
Concept Snapshot
Auth state change listeners track user sign-in and sign-out events.
Register a listener with supabase.auth.onAuthStateChange(callback).
Callback receives event type and session info.
Update app state and UI based on session presence.
Listener runs on every auth event including sign-in, sign-out, and token refresh.
Full Transcript
When you start your app, you set up a listener to watch for authentication changes. This listener waits for events like user sign-in or sign-out. When the user signs in, the listener runs a function that receives the event type and session details. The app then updates its state to show the user is logged in. If the user signs out, the listener runs again, session becomes null, and the app updates to logged out. The listener also runs on token refresh events to keep the session current. This way, your app always knows the user's auth status and can update the interface accordingly.