Node.js Express, Angular 10: JWT Authentication & Authorization example

In this tutorial, we will learn how to build a full stack Node.js Express + Angular 10 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 10 with HttpInterceptor and Router. We’ll also perform Form validation on UI.

Related Post: Node.js + MongoDB: User Authentication & Authorization with JWT


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

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

Node.js Express Angular 10 Authentication example

It will be a full stack, with Node.js Express for back-end and Angular 10 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 10 Client App.

– Anyone can access a public page before logging in:

node-js-express-angular-10-jwt-authentication-public-content

– A new User can signup:

node-js-express-angular-10-jwt-authentication-signup

– Login with legal account:

node-js-express-angular-10-jwt-authentication-signin

Profile page:

node-js-express-angular-10-jwt-authentication-profile

– UI for admin role:

node-js-express-angular-10-jwt-authentication-authorized-login

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

node-js-express-angular-10-jwt-authentication-unauthorized

Flow for User Registration and User Login

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

node-js-express-angular-10-jwt-authentication-flow

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

Overview

Our Node.js Express Application can be summarized in the diagram below:

node-js-express-angular-10-jwt-authentication-backend-architecture

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.

Technology

  • Express 4
  • bcryptjs 2.4.3
  • jsonwebtoken 8.5.1
  • Sequelize 5.21.3
  • MySQL

Project Structure

This is directory structure for our Node.js Express application:

node-js-express-angular-10-jwt-authentication-backend-project-structure

config

  • configure MySQL database & Sequelize
  • configure Auth Key

routes

  • auth.routes.js: POST signup & signin
  • user.routes.js: GET public & protected resources

middlewares

  • verifySignUp.js: check duplicate Username or Email
  • authJwt.js: verify Token, check User roles in database

controllers

  • auth.controller.js: handle signup & signin actions
  • user.controller.js: return public & protected content

models for Sequelize Models

  • user.model.js
  • role.model.js

server.js: import and initialize neccesary modules and routes, listen for connections.

Implementation

You can find step by step to implement this Node.js App in the post:
Node.js – JWT Authentication & Authorization example

Or you can use MongoDB database instead:
Node.js + MongoDB: JWT Authentication & Authorization example

Front-end with Angular 10, HttpInterceptor and Router

Overview

Our Angular App can be summarized in component diagram below:

node-js-express-angular-10-jwt-authentication-frontend-overview

– The App component is a container using Router. It gets user token & user information from Browser Session Storage via token-storage.service. Then the navbar now can display based on the user login state & roles.

Login & Register components have form for submission data (with support of Form Validation). They use token-storage.service for checking state and auth.service for sending signin/signup requests.

auth.service uses Angular HttpClient ($http service) to make authentication requests.
– every HTTP request by $http service will be inspected and transformed before being sent by auth-interceptor.

Home component is public for all visitor.

Profile component get user data from Session Storage.

BoardUser, BoardModerator, BoardAdmin components will be displayed depending on roles from Session Storage. In these components, we use user.service to get protected resources from API.

Technology

– Angular 10
– RxJS 6

Project Structure

The structure of Angular front-end project is simple:

node-js-express-angular-10-jwt-authentication-frontend-project-structure

Because I’ve shown component diagram, you can understand it properly without any explanation.

Implementation

You can find step by step to implement this Angular App in the post:
Angular 10 JWT Authentication example with Web Api

Older version: Angular 8 JWT Authentication with HttpInterceptor and Router

Further Reading

Conclusion

Now we have an overview of Node.js Express + Angular 10 Authentication and Authorization example using JWT, HttpInterceptor, Router, Form Validation along with flow for signup/login actions.

We also take a look at Node.js Express server architecture for JWT Authentication using jsonwebtoken & Sequelize, as well as Angular project structure for building a front-end app working with JWT.

Next tutorials will show you more details about how to implement this interesting system:
Back-end
– Front-end:

This Angular Client also works well with back-end in the post:
Node.js + MongoDB: User Authentication & Authorization with JWT

Happy learning, see you again!

One thought to “Node.js Express, Angular 10: JWT Authentication & Authorization example”

  1. A big thumbs up and a round of applause for you dude. I will learn this tutorials of you keep posting more tutorials please…

Leave a Reply

Your email address will not be published. Required fields are marked *