In our auth.service.ts class, let's start by injecting the JwtHelperService provided by the library into our constructor.
Update the method in our service to check if a user has an unexpired token or not:
Decoding the token
Next, we'll add a property to hold the information in the token and the JwtHelper.
Update the login() method to use the jwtHelperService:
Displaying the Username in the Nav
In the nav.component.html, let's use the AuthService to display the username when a user is logged in:
What we currently have:
Let's change it to:
The decodedToken? has a conditional null operator. If the page loads and it can't find this - it won't crash.
Retrieving User Information ASAP
Right now, if we're logged in and then refresh the page - we lose our username in the navbar and our information.
Instead of just loading the information in the navbar, we'll try loading all user information as soon as the root component of the application is initialized (before the nav component).
In app.component.ts, update it to the following:
Now, we should be able to refresh the page without losing our status and information.