Node.js + Angular 11: JWT Authentication & Authorization example
Tags: angular, node, webdev, javascript
Published: 2020-12-19 08:58:47

In this tutorial, we will learn how to build a full stack Node.js Express + Angular 11 Authentication example. The back-end server uses Node.js Express with jsonwebtoken for JWT Authentication & Authorization, Sequelize for interacting with MySQL database. The front-end will be created with Angular 11, HttpInterceptor and Router. We’ll also perform Form validation on UI.

## JWT (JSON Web Token)
Comparing with Session-based Authentication that need to store Session on Cookie, the big advantage of Token-based Authentication is that we store the JSON Web Token (JWT) on Client side: Local Storage for Browser, Keychain for IOS and SharedPreferences for Android… So we don’t need to build another backend project that supports Native Apps or an additional Authentication module for Native App users.

![in-depth-introduction-jwt-token-based-authentication](https://dev-to-uploads.s3.amazonaws.com/i/dnmsdyxojozvxgx4wtve.png)

There are three important parts of a JWT: Header, Payload, Signature. Together they are combined to a standard structure: `header.payload.signature`.

The Client typically attaches JWT in **x-access-token** header:
```
x-access-token: [header].[payload].[signature]
```
For more details, you can visit:
[In-depth Introduction to JWT-JSON Web Token](https://bezkoder.com/jwt-json-web-token/)

## Node.js Express Angular 11 Authentication example
It will be a full stack, with Node.js Express for back-end and Angular 11 for front-end. The access is verified by JWT Authentication.

- User can signup new account, login with username & password.
- Authorization by the role of the User (admin, moderator, user)

## Screenshots
The images below shows screenshots of our Angular 11 Client App.

– Anyone can access a public page before logging in:

![jwt-authentication-node-js-angular-11-login-registration-public-page](https://dev-to-uploads.s3.amazonaws.com/i/o5eevsqr84k84z8nsydc.png)

– A new User can signup:

![jwt-authentication-node-js-angular-11-login-registration-signup-page](https://dev-to-uploads.s3.amazonaws.com/i/8hmoq6r96s5wcef7z937.png)

– Registration form validation:

![jwt-authentication-node-js-angular-11-login-registration-form-validation](https://dev-to-uploads.s3.amazonaws.com/i/fj3a48u9f186qjhvnrd1.png)

– Login with legal account:

![jwt-authentication-node-js-angular-11-login-registration-signin-page](https://dev-to-uploads.s3.amazonaws.com/i/lk5e652t64ds14dspjnq.png)

– **Profile** page:

![jwt-authentication-node-js-angular-11-login-registration-profile-page](https://dev-to-uploads.s3.amazonaws.com/i/1lcga8r285dqqhse9mc9.png)

– UI for **admin** role:

![jwt-authentication-node-js-angular-11-login-registration-authorization](https://dev-to-uploads.s3.amazonaws.com/i/8aakdcv058dt9oxj4psq.png)

– If a User who doesn’t have Admin role tries to access **Admin**/**Moderator** Board page:

![jwt-authentication-node-js-angular-11-login-registration-unauthorized](https://dev-to-uploads.s3.amazonaws.com/i/s1hq6bkqdbrtq2gf9059.png)

## Demo
This is full Angular + Node.js Express JWT Authentication & Authorization App Demo (with form validation, check signup username/email duplicates, test authorization with 3 roles: Admin, Moderator, User).

{% youtube TZP0jMugC3k %}

In the video, we use Angular 10, but the logic and UI are the same as this Angular version 11.

## Flow for User Registration and User Login
The diagram shows flow of User Registration, User Login and Authorization process.

![jwt-authentication-node-js-angular-11-login-registration-flow](https://dev-to-uploads.s3.amazonaws.com/i/037lz1dsuyokba82gk3v.png)

We have 2 endpoints for authentication:

- `api/auth/signup` for User Registration
- `api/auth/signin` for User Login
A legal JWT must be added to HTTP **x-access-token** Header if Client accesses protected resources.

## Back-end with Node.js Express & Sequelize
Our Node.js Express Application can be summarized in the diagram below:

![jwt-authentication-node-js-angular-11-login-registration-architecture](https://dev-to-uploads.s3.amazonaws.com/i/c0688jcrc7aeabmvgtqw.png)

Via Express routes, **HTTP request** that matches a route will be checked by **CORS Middleware** before coming to **Security** layer.

**Security** layer includes:

- JWT Authentication Middleware: verify SignUp, verify token
- Authorization Middleware: check User’s roles with record in database

If these middlewares throw any error, a message will be sent as HTTP response.

**Controllers** interact with MySQL Database via **Sequelize** and send **HTTP response** (token, user information, data based on roles…) to client.

## Front-end with Angular 11, HttpInterceptor and Router

![jwt-authentication-node-js-angular-11-login-registration-components](https://dev-to-uploads.s3.amazonaws.com/i/agc9opceqsbukc6qqxqx.png)

For more details, please visit:
[https://bezkoder.com/node-js-angular-11-jwt-authentication/](https://bezkoder.com/node-js-angular-11-jwt-authentication/)

# Further Reading
- [Node.js + MongoDB: JWT Authentication & Authorization](https://bezkoder.com/node-js-mongodb-auth-jwt/)
- [Node.js + PostgreSQL: JWT Authentication & Authorization](https://bezkoder.com/node-js-jwt-authentication-postgresql/)

Fullstack CRUD App:
- [Angular + Node.js Express + MySQL example](https://bezkoder.com/angular-11-node-js-express-mysql/)
- [Angular + Node.js Express + PostgreSQL example](https://bezkoder.com/angular-11-node-js-express-postgresql/)
- [Angular + Node.js Express + MongoDB example](https://bezkoder.com/angular-11-mongodb-node-js-express/)

Using HttpOnly Cookies for JWT: (updated)
[Node Express + Angular: Authentication & Authorization example](https://www.bezkoder.com/node-js-angular-13-jwt-auth/)