In this tutorial, we will learn how to build a full stack Node.js Express + Angular 14 JWT Authentication (Login, Registration) and Role based Authorization example. The back-end server uses Node.js Express with jsonwebtoken for Rest APIs, Sequelize for interacting with MySQL database. The front-end will be created with Angular 14, HttpInterceptor and Router. We’ll also perform Form validation on UI.
MEAN stack instead:
Angular 14 + Node + MongoDB: Login and Registration example
Related Posts:
– JWT tutorial: In-depth Introduction to JSON Web Token
– Angular + Node.js Express: File Upload example
– Node.js Express and MongoDB: Login and Registration example
Fullstack:
– Angular + Node Express + MySQL example
– Angular + Node Express + PostgreSQL example
– Angular + Node Express + MongoDB example
Run both projects in one place:
How to Integrate Angular with Node.js Restful Services
Newer version:
– Angular 15 + Node.js: JWT Authentication and Authorization example
– Angular 16 + Node.js: JWT Authentication and Authorization example
Contents
Angular 14 Node.js JWT Auth example
It will be a full stack, with Angular 14 for front-end and Node.js Express for back-end with MySQL database. The access is verified by JWT Authentication in HttpOnly Cookies.
- User can signup new account (registration), login with username & password.
- Role based Authorization (admin, moderator, user)
Screenshots
The images below shows screenshots of our Angular 14 Client App.
– Anyone can access a public page before logging in:
– New user registration:
Signup Successfully:
– Signup form validation:
If you want to know more details about Form Validation, please visit:
– Angular 14 Template Driven Forms Validation example
– Angular 14 Reactive Forms Validation example
– Login with legal account:
Login Successfully:
HttpOnly Cookie set by the Server:
– Profile page (for successful Login):
– For Authorization (Moderator account login), the navigation bar will change by authorities:
HttpOnly Cookie is sent automatically with HTTP Request:
– Browser Local/Session Storage for storing user information:
– If a User who doesn’t have Admin role tries to access Admin Board page:
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).
In the video, we use Angular 10 and HTTP Authorization Header for JWT, but the logic and UI are the same as this Angular version 14 and HttpOnly Cookies.
Flow for Authentication and Authorization
The diagram shows flow for User Registration, User Login and Resource access process.
It’s not too difficult to understand. We have 3 endpoints for authentication:
api/auth/signup
for User Registrationapi/auth/signin
for User Loginapi/auth/signout
for User Logout
This Angular Client uses JWT in Cookies while sending request to protected resources (Authorization).
Node.js Express for Backend
Overview
Our Node.js Express Application can be summarized in the diagram below:
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.18.2
- bcryptjs 2.4.3
- cookie-session 1.4.0
- jsonwebtoken 8.5.1
- Sequelize 6.11.0
- MySQL
Project Structure
This is directory structure for our Node.js Express application:
– config
- configure MySQL database & Sequelize
- configure Auth Key
– routes
- auth.routes.js: POST signup, signin & signout
- 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 & signout actions
- user.controller.js: return public & protected content
– models for Sequelize Models
- user.model.js
- role.model.js
– server.js: import and initialize necessary modules and routes, listen for connections.
Implementation and Source Code
You can find step by step to implement this Node.js App in the post (with Github):
Node.js Express: Login example with JWT and MySQL
Or you can use MongoDB database instead:
Node.js Express: Login example with JWT and MongoDB
Before running the backend server, you need to add minor configuration:
/* In server.js */
// app.use(cors());
app.use(
cors({
credentials: true,
origin: ["http://localhost:8081"],
})
);
Angular 14 for Frontend
Overview
Our Angular 14 App can be summarized in component diagram below:
Let’s try to understand it right now.
– The App
component is a container using Router
. It gets user user information from Browser Session Storage via 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 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 (with JWT in HttpOnly Cookie).
Technology
- Angular 14
- RxJS 7
- Angular CLI 14
- Bootstrap 4
Project Structure
This is the folder structure of our Angular front-end project:
You can understand it properly without any explanation because we’ve looked at the overview before.
Implementation and Source Code
You can find step by step to implement this Angular 14 Client (with Github) in the post:
Angular 14 JWT Authentication & Authorization example
Further Reading
- https://sequelize.org/v6/
- https://www.npmjs.com/package/jsonwebtoken
- Angular HttpInterceptor
- In-depth Introduction to JWT-JSON Web Token
Fullstack CRUD Application:
– Angular + Node Express + MySQL example
– Angular + Node Express + PostgreSQL example
– Angular + Node Express + MongoDB example
Source Code
The complete source code for this tutorial can be found at Angular Node Express Github.
Conclusion
Now we have an overview of Node.js Express + Angular 14 Authentication and Authorization example using JWT, HttpInterceptor, Router, Form Validation along with flow for registration and 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:
– Part 2: Node.js Express Back-end
– Part 3: Angular 14 Front-end
This Angular Client also works well with back-end in the post:
Node.js Express: Login example with JWT and MongoDB
You will want to know how to run both projects in one place:
How to Integrate Angular with Node.js Restful Services
Happy learning, see you again!