# Managing auth state in your Angular Firebase app

<span class="r"></span>

# Managing auth state in your Angular Firebase app.


<noscript><img alt="Image for post" class="s t u by ai" src="https://miro.medium.com/max/1252/1*NnQ1RFFyigISAmsqw5S3XA.jpeg" width="626" height="354" srcSet="https://miro.medium.com/max/552/1*NnQ1RFFyigISAmsqw5S3XA.jpeg 276w, https://miro.medium.com/max/1104/1*NnQ1RFFyigISAmsqw5S3XA.jpeg 552w, https://miro.medium.com/max/1252/1*NnQ1RFFyigISAmsqw5S3XA.jpeg 626w" sizes="626px"/></noscript>



<span class="r jb jc jd je jf jg jh ji jj df">F</span>irebase authentication usually comes in handy when building any type of application. It helps manage your app’s users, auth state, as well as track user sessions.

In this article we’ll go through tracking authentication state in your angular application and emitting changes to other components using Firebase’s onAuthStateChanged Observable and an RXJS Subject.

let’s dive in……..

In the [signin.component.ts](https://gist.github.com/chani24/1dca2d5d2f315fa0500c1cab0dff289a#file-signin-component-ts) , we get login data from the form in our template, which we will then pass to our auth service for authentication.

In our [auth.service.ts](https://gist.github.com/chani24/4b9d75f79d14ec239d91975683573f2a#file-auth-service-ts), we import BehaviourSubject and Observable from RXJS. BehavourSubject is a special type of Observable that allows values to be multicast to many Observers. We also import AngularFireAuth from Angular Fire and auth from Firebase/app.

The following steps explain the process.

Step 1: Initialize a property, currentUser.

Step 2 : Initialize a new BehaviourSubject called authStatusSub and pass currentUser as it’s first value.

Step 3 : Initialize a property, currentAuthStatus which returns authStatusSub as an Obvservable that can be observed across components.

Step 4 : Create a method called authStatusListener and used onAuthStateChanged to check auth state. If a user is Logged in, we call authStatusSub.next and pass in the current user credentials however if no user is logged in, we pass null into authStatusSub.next.

authStatusSub.next passes the current value to all Observers. See [https://rxjs-dev.firebaseapp.com/guide/subject](https://rxjs-dev.firebaseapp.com/guide/subject) for more explanation.

Let’s proceed to listen to it’s emissions in another component.

In [header.component.ts](https://gist.github.com/chani24/d9f87683c7d1f7e16c077f1c359c7a42#file-header-component-ts), we subscribe to currentAuthStatus from our auth service and pass it’s value into an earlier initialized isAuthenticated property.

In [header.component.html](https://gist.github.com/chani24/d9f87683c7d1f7e16c077f1c359c7a42#file-header-component-html), we use conditionals to check if isAuthenticated is truthy or falsy, and then proceed to display elements in accordance.

We’ve been able to successfully track auth state and emit new changes in our auth status across different components. We’ve also been able to subscribe and get emitted values and use in our application logic.

Goodluck coding….
