In this user story, we will display the username of the logged in user on the screen. To implement this user story, we will take the following steps:
Add a method to the authenticator module to extract and return the username from the JWT
Update the view to display the logged in user in the navigation bar
Let's start by adding a method to the authenticator module. We will name this method loggedInUser
and make it a pure observable so that it can be used in the view. We will extract the username form the JWT we received from the server. Recall the JWT structure described earlier in this chapter. The JWT is base64 encoded with dot (.
) as the separator between head, body, and signature. To retrieve the username, we will split the token and then base64 decode the body. The body of the token contains the userName
attribute, which the method should return. Our implementation of the pure computed observable looks similar to this:
/* return the user name of the logged in...