Consider this Supabase auth state change listener code snippet:
supabase.auth.onAuthStateChange((event, session) => {
if (event === 'SIGNED_OUT') {
console.log('User signed out');
}
});What will be printed when the user signs out?
supabase.auth.onAuthStateChange((event, session) => {
if (event === 'SIGNED_OUT') {
console.log('User signed out');
}
});Check the event name string for the sign-out event.
The listener triggers once when the auth state changes. When the event is 'SIGNED_OUT', the message prints once.
In the Supabase auth state change listener:
supabase.auth.onAuthStateChange((event, session) => { /* ... */ });What does the session parameter contain?
supabase.auth.onAuthStateChange((event, session) => { /* ... */ });Think about what data you need to access user info after sign-in.
The session parameter holds the current session data, including tokens and user details, or null if no user is signed in.
Supabase's onAuthStateChange returns a subscription object with an unsubscribe method.
Which option correctly unsubscribes the listener?
const { data: authListener } = supabase.auth.onAuthStateChange((event, session) => {
console.log(event);
});
// Unsubscribe code hereCheck the method name on the subscription object returned.
The subscription object has an unsubscribe() method to stop listening to auth changes.
If you add an auth state change listener in a single-page app but never unsubscribe it on page unload, what risk arises?
Think about what happens when event listeners accumulate without removal.
Not unsubscribing causes memory leaks as listeners accumulate, slowing the app and wasting resources.
You want to keep user auth state synchronized across multiple browser tabs using Supabase auth listeners.
Which approach best achieves this?
Consider browser features that allow communication between tabs.
Each tab listens for auth changes and uses localStorage events to notify others, keeping state in sync without reloads.