0
0
Firebasecloud~20 mins

Auth state observer in Firebase - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Firebase Auth Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
service_behavior
intermediate
2:00remaining
What happens when Firebase auth state changes?

Consider the Firebase auth state observer below. What will be logged when a user signs out?

firebase.auth().onAuthStateChanged(user => {
  if (user) {
    console.log('User signed in:', user.uid);
  } else {
    console.log('No user signed in');
  }
});
Firebase
firebase.auth().onAuthStateChanged(user => {
  if (user) {
    console.log('User signed in:', user.uid);
  } else {
    console.log('No user signed in');
  }
});
ALogs 'No user signed in'
BLogs 'User signed in:' followed by the user ID
CThrows an error because user is undefined
DDoes nothing, no logs appear
Attempts:
2 left
💡 Hint

Think about what the observer receives when the user signs out.

Configuration
intermediate
2:00remaining
Which code correctly unsubscribes from Firebase auth state observer?

You want to stop listening to auth state changes to avoid memory leaks. Which option correctly unsubscribes the observer?

Firebase
const unsubscribe = firebase.auth().onAuthStateChanged(user => {
  console.log(user ? 'Signed in' : 'Signed out');
});

// How to unsubscribe?
Afirebase.auth().removeListener('onAuthStateChanged', unsubscribe);
Bfirebase.auth().offAuthStateChanged(unsubscribe);
Cfirebase.auth().onAuthStateChanged(null);
Dunsubscribe();
Attempts:
2 left
💡 Hint

Check the return value of onAuthStateChanged.

Architecture
advanced
2:00remaining
Best architecture to handle auth state in a single-page app

You build a single-page app using Firebase auth. Which architecture best ensures UI updates correctly on auth state changes?

ACall <code>onAuthStateChanged</code> inside every component that needs user info
BCall <code>onAuthStateChanged</code> once at app start and update global state accordingly
CUse <code>getCurrentUser()</code> repeatedly on user actions instead of observer
DReload the page on every auth state change to refresh UI
Attempts:
2 left
💡 Hint

Think about performance and consistency of user state.

security
advanced
2:00remaining
What security risk arises if auth state observer is not used properly?

If your app does not listen to auth state changes and relies only on initial user info, what risk occurs?

AUser credentials get exposed in console logs
BFirebase throws an authentication error immediately
CUser might access protected content after signing out until page reload
DApp crashes due to missing user object
Attempts:
2 left
💡 Hint

Consider what happens if UI does not update after sign out.

🧠 Conceptual
expert
2:00remaining
Why does Firebase auth state observer trigger twice on page load?

When your app loads, the onAuthStateChanged observer fires twice: once with null and then with the user object. Why?

AFirebase first emits a default null state before confirming user session asynchronously
BIt is a bug in Firebase SDK causing duplicate events
CThe observer is registered twice accidentally in code
DBrowser caches cause the observer to fire twice
Attempts:
2 left
💡 Hint

Think about how Firebase loads user session data.