Logging In

Learn how to write unit tests covering application login.

We need to test application login thoroughly because this is a critical piece of the system. In Angular, we usually have a login component that takes care of the appearance of the login form. This login component will delegate the authentication logic to an authentication class responsible for communicating with our server and storing our login details. We’ll need to write unit tests to cover the login form and unit tests to cover our authentication class.

Login authentication using JWT

For authentication with JSON Web Tokens (JWT), we present the user with a login form, where they need to enter a username and password. We make an API request to pass these details to the server, which will validate the login request. If the login details are correct, the server will respond with a JWT token. The frontend needs to store this JWT token and use it to authenticate future requests to the server. When the frontend makes API requests to the server, it can simply pass the JWT token, which the server will verify. The frontend needs to store the JWT token using a solution such as a cookie, session storage, or local storage.

In short, when a JWT is received, our application must do the following actions:

  • Store the JWT.
  • Add the JWT to the request headers.

JWTs

JSON Web Tokens (JWTs) are an industry ...