Auth state change listeners help your app know when a user logs in or out. This way, your app can update what it shows without needing a refresh.
Auth state change listeners in Supabase
const { data: authListener } = supabase.auth.onAuthStateChange((event, session) => {
// Your code here
});The event tells you what happened, like 'SIGNED_IN' or 'SIGNED_OUT'.
The session contains user info if logged in, or null if logged out.
const { data: authListener } = supabase.auth.onAuthStateChange((event, session) => {
if (event === 'SIGNED_IN') {
console.log('User signed in:', session.user.email);
}
});const { data: authListener } = supabase.auth.onAuthStateChange((event, session) => {
if (event === 'SIGNED_OUT') {
console.log('User signed out');
}
});const { data: authListener } = supabase.auth.onAuthStateChange((event, session) => {
console.log('Auth event:', event);
console.log('Session:', session);
});This program sets up a listener for authentication state changes using Supabase. It logs when a user signs in or out. It runs for 60 seconds before stopping the listener.
import { createClient } from '@supabase/supabase-js'; const supabaseUrl = 'https://xyzcompany.supabase.co'; const supabaseKey = 'public-anonymous-key'; const supabase = createClient(supabaseUrl, supabaseKey); console.log('Starting auth listener...'); const { data: authListener } = supabase.auth.onAuthStateChange((event, session) => { console.log(`Auth event detected: ${event}`); if (event === 'SIGNED_IN' && session) { console.log(`User signed in: ${session.user.email}`); } else if (event === 'SIGNED_OUT') { console.log('User signed out'); } else { console.log('Other auth event or no session'); } }); // Simulate waiting for auth events setTimeout(() => { console.log('Stopping auth listener...'); authListener.subscription.unsubscribe(); }, 60000);
The listener runs in the background and triggers your code whenever the auth state changes.
Remember to unsubscribe the listener when it is no longer needed to avoid memory leaks.
Time complexity is minimal since it reacts only to events, not polling.
Auth state change listeners keep your app updated with user login status.
Use them to show or hide content based on whether a user is signed in.
Always unsubscribe listeners when done to keep your app efficient.
Practice
Solution
Step 1: Understand auth state change listener role
An auth state change listener watches for user login or logout events.Step 2: Identify its main use in app behavior
It updates the app UI or logic based on whether the user is signed in or out.Final Answer:
To detect when a user logs in or logs out and update the app accordingly -> Option CQuick Check:
Auth listener updates app on login/logout [OK]
- Thinking it sends emails automatically
- Confusing with data storage functions
- Assuming it encrypts passwords
Solution
Step 1: Recall Supabase auth listener method
The correct method to listen for auth changes isonAuthStateChange.Step 2: Verify method signature
It takes a callback with parameterseventandsession.Final Answer:
supabase.auth.onAuthStateChange((event, session) => { /* handler */ }) -> Option BQuick Check:
Use onAuthStateChange for auth listeners [OK]
- Using non-existent methods like listenAuthChange
- Confusing subscribe or watchStateChange methods
- Incorrect callback parameters
const { data: authListener } = supabase.auth.onAuthStateChange((event, session) => {
console.log(event);
});
// User signs out here
supabase.auth.signOut();Solution
Step 1: Understand event on user sign out
When a user signs out, the event emitted is "SIGNED_OUT".Step 2: Check console.log output
The listener logs the event string, so it will print "SIGNED_OUT".Final Answer:
"SIGNED_OUT" -> Option AQuick Check:
Sign out triggers SIGNED_OUT event [OK]
- Expecting SIGNED_IN on sign out
- Thinking USER_UPDATED triggers on sign out
- Assuming no event is logged
const authListener = supabase.auth.onAuthStateChange((event, session) => {
if(event === 'SIGNED_IN') {
console.log('User signed in');
}
});
// Later in code
authListener.unsubscribe();Solution
Step 1: Check how onAuthStateChange returns subscription
It returns an object with adataproperty containing the subscription.Step 2: Verify unsubscribe method call
Unsubscribe should be called onauthListener.data.subscription.unsubscribe(), not directly onauthListener.unsubscribe().Final Answer:
authListener.unsubscribe() is incorrect; should use authListener.data.subscription.unsubscribe() -> Option AQuick Check:
Unsubscribe via authListener.data.subscription.unsubscribe() [OK]
- Calling unsubscribe directly on listener object
- Misnaming event strings
- Skipping unsubscribe causing memory leaks
Solution
Step 1: Identify events to trigger UI update
Only 'SIGNED_IN' and 'SIGNED_OUT' events should update the UI.Step 2: Check conditional logic in each option
supabase.auth.onAuthStateChange((event, session) => { if(event === 'SIGNED_IN' || event === 'SIGNED_OUT') { updateUI(session); } }); correctly checks for these two events and callsupdateUIaccordingly.Final Answer:
supabase.auth.onAuthStateChange((event, session) => { if(event === 'SIGNED_IN' || event === 'SIGNED_OUT') { updateUI(session); } }); -> Option DQuick Check:
Update UI only on SIGNED_IN or SIGNED_OUT [OK]
- Updating UI on all events without filtering
- Using wrong event names like USER_UPDATED
- Negating conditions incorrectly
Ready to prove it?
Pass the test and this pattern moves unknown
