Track and Handle Authentication Status

Learn to handle authentication state changes in Firebase.

We’ve learned to sign up users, sign them out, and then sign them back in. Now, it’s time to learn how to set up a real-time listener to track users’ authentication status. This could be useful when we wish to perform certain actions based on whether or not a user is signed in.

Authentication state observers

For components of our application that need information about the signed-in user, we can attach an observer that is called whenever the user’s sign-in state changes. The Firebase Authentication SDK contains the onAuthStateChanged function that we can attach to the Auth object to listen for any changes in the authentication status. This function monitors users’ authentication state in real time and provides information about the current user.

The onAuthStateChanged function is imported from the firebase/auth subpackage, and takes the initialized auth instance as its first parameter. Then, it takes a callback function that represents the user instance, as its second parameter:

Get hands-on with 1400+ tech skills courses.