0
0
Supabasecloud~20 mins

Auth state change listeners in Supabase - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Supabase Auth Listener Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
service_behavior
intermediate
2:00remaining
What happens when a user signs out in Supabase auth listener?

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
supabase.auth.onAuthStateChange((event, session) => {
  if (event === 'SIGNED_OUT') {
    console.log('User signed out');
  }
});
AThe console prints 'User signed out' exactly once when the user signs out.
BThe console prints 'User signed out' every second after the user signs out.
CNothing is printed because the event name is incorrect.
DThe console prints 'User signed out' only if the session is null.
Attempts:
2 left
💡 Hint

Check the event name string for the sign-out event.

🧠 Conceptual
intermediate
2:00remaining
What does the session parameter represent in onAuthStateChange?

In the Supabase auth state change listener:

supabase.auth.onAuthStateChange((event, session) => { /* ... */ });

What does the session parameter contain?

Supabase
supabase.auth.onAuthStateChange((event, session) => { /* ... */ });
AThe previous session object before the auth state changed.
BThe event type string like 'SIGNED_IN' or 'SIGNED_OUT'.
CA boolean indicating if the user is authenticated.
DThe current user's session object including access token and user info, or null if signed out.
Attempts:
2 left
💡 Hint

Think about what data you need to access user info after sign-in.

Configuration
advanced
2:00remaining
Which code correctly unsubscribes from the auth state change listener?

Supabase's onAuthStateChange returns a subscription object with an unsubscribe method.

Which option correctly unsubscribes the listener?

Supabase
const { data: authListener } = supabase.auth.onAuthStateChange((event, session) => {
  console.log(event);
});

// Unsubscribe code here
AauthListener.removeListener();
BauthListener.unsubscribe();
Csupabase.auth.offAuthStateChange(authListener);
Dsupabase.auth.onAuthStateChange(null);
Attempts:
2 left
💡 Hint

Check the method name on the subscription object returned.

security
advanced
2:00remaining
What security risk exists if you do not unsubscribe auth listeners on page unload?

If you add an auth state change listener in a single-page app but never unsubscribe it on page unload, what risk arises?

AThe listener will block all network requests until unsubscribed.
BUser credentials get exposed to third-party sites automatically.
CMemory leaks causing increased resource use and potential slowdowns.
DThe app will automatically sign out the user after a timeout.
Attempts:
2 left
💡 Hint

Think about what happens when event listeners accumulate without removal.

Architecture
expert
3:00remaining
How to architect a global auth state listener for multi-tab sync in Supabase?

You want to keep user auth state synchronized across multiple browser tabs using Supabase auth listeners.

Which approach best achieves this?

AUse <code>onAuthStateChange</code> in each tab and broadcast changes via <code>localStorage</code> events to sync state.
BUse server-side polling every 5 seconds to check auth state for all tabs.
CStore the session in a cookie and reload tabs on cookie change to sync state.
DUse a single global listener in the main tab and block other tabs from listening.
Attempts:
2 left
💡 Hint

Consider browser features that allow communication between tabs.