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.auth().onAuthStateChanged(user => {
if (user) {
console.log('User signed in:', user.uid);
} else {
console.log('No user signed in');
}
});Think about what the observer receives when the user signs out.
When a user signs out, the observer receives null as the user parameter, so the else block runs, logging 'No user signed in'.
You want to stop listening to auth state changes to avoid memory leaks. Which option correctly unsubscribes the observer?
const unsubscribe = firebase.auth().onAuthStateChanged(user => {
console.log(user ? 'Signed in' : 'Signed out');
});
// How to unsubscribe?Check the return value of onAuthStateChanged.
The onAuthStateChanged method returns an unsubscribe function. Calling it stops the observer.
You build a single-page app using Firebase auth. Which architecture best ensures UI updates correctly on auth state changes?
Think about performance and consistency of user state.
Registering a single observer at app start centralizes auth state management and avoids multiple listeners or unnecessary reloads.
If your app does not listen to auth state changes and relies only on initial user info, what risk occurs?
Consider what happens if UI does not update after sign out.
Without listening to auth state changes, the app may still show protected content after sign out until refreshed, risking unauthorized access.
When your app loads, the onAuthStateChanged observer fires twice: once with null and then with the user object. Why?
Think about how Firebase loads user session data.
Firebase initially emits null because it does not know user state yet, then asynchronously loads the session and emits the actual user.