How it works...
Our CurrentUserService tracks the current user's status through a special type of Observable provided by RXJS called a BehaviorSubject. BehaviorSubject has the special property of always having a subject associated with it that changes over time. When our current user status changes, any other part of our application that gets its value from our getUser method as an Observable will be updated via data binding. By default, we set this object to an empty User instance, but, upon login we pass in a new value using the next method of BehaviorSubject. Using observables for these sorts of long-running dynamic values can be useful for getting the asynchronous state of an object whenever it changes.
Our login method uses Angular's ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access