Using JWT authentication with Express and Angular
Managing authentication state via a session cookie is a valid strategy, but an increasingly common approach is to use a compact JSON Web Token or JWT to manage authentication state between the back-end and frontend layers of our web application. We can store our user information inside a JWT so that the client can restore its authenticated state upon reload.
Getting ready
To use JWT in our application, we will need to install a library both in our Express web server and in our Angular application. For our web server, we will use jwt-express, a very handy and easy-to-use JWT library for Express. We will use this library to create our JWT, as well as refresh it automatically when we get follow-up requests within a 15-minute expiration window:
npm install jwt-express --save
For Angular, we will install the popular angular2-jwt
library. We will use this library to decode our JWT and read our user details from it, as well as make sure that its not...