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

Jorge Vergara
InstructorJorge Vergara
Share this video with your friends

Social Share Links

Send Tweet
Published 3 years ago
Updated 3 years 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.

egghead
egghead
~ 3 minutes ago

Member comments are a way for members to communicate, interact, and ask questions about a lesson.

The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io

Be on-Topic

Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.

Avoid meta-discussion

  • This was great!
  • This was horrible!
  • I didn't like this because it didn't match my skill level.
  • +1 It will likely be deleted as spam.

Code Problems?

Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context

Details and Context

Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!

Markdown supported.
Become a member to join the discussionEnroll Today