Choose Between authState or user to Subscribe to Firebase Authentication State Changes

Jorge Vergara
InstructorJorge Vergara

Share this video with your friends

Send Tweet
Published a year ago
Updated a year ago

You'll learn the 2 Observables that Firebase Authentication provides to subscribe to the authentication state of your application, authState and user.

You'll see how they both return the same User object from Firebase Authentication.

And you'll learn that the difference is that authState triggers when the user logs in, or logs out of the application, while user is called both of those times, and also is called when Firebase refreshes the user's authentication token.

As a personal guideline, if I need to do an operation when the user's token is refreshed (for example, update a 3rd party service) I'll use user, if not, I'll default to authState.

Jorge Vergara: [0:00] There are two ways to subscribe to the authentication state of your application when using Firebase, the AuthState and the user package, both available in Firebase Authentication. To test this, we are using an Ionic with Angular application, but you can use any type of web application you want.

[0:18] In the constructor for AuthenticationService, we are subscribing to both AuthState and user. When we reload the page, we notice that they are both logged as null because there is no current user logged in to our application. If we log the user in, we can see that the subscriber triggers. We get the same information in both.

[0:43] They both work the same way. The main difference they have is when they are called. AuthState is called whenever the user logs in or logs out. User is called whenever the user logs in, logs out, or when Firebase updates the user's authentication token.