I created a service for my angular app like so:
export class AuthService {
public currentUser: Subject<firebase.User> = new Subject();
public currentUserId: Subject<string> = new Subject();
constructor(private auth: AngularFireAuth){
auth.authState.subscribe(r => {
this.currentUser.next(r); // the user object
this.currentUserId.next(r.uid); // the user id
});
}
}
I use this service in my components when routing occurs.
When the user first arrives at a page from their browser, the other components are successfully notified of the authentication changes. However, when a user clicks a link to go to another page, the two observables don't fire and the listening components aren't notified of the authentication state changes. The only way to work is if the user refreshes their browser since they seem to only fire when the user lands on their first page only. After that, I lost the user information and am never informed again.
How can I have the two observables fire every time a page navigation occurs? Or is this even the correct approach to doing this?
null
, then subscribe to theSubejct
s. Doing this for every component is annoying. – AskYous