Skip to main content

BezKoder

  • Courses
  • Full Stack
  • Spring
  • Node
  • Vue
  • Angular
  • React
  • Firebase
  • Django
  • Dart
  • JsonFormatter
  • Categories

Angular 15 + Node.js: Login & Registration example with JWT

Last modified: December 24, 2022 bezkoder Angular, Full Stack, Node.js, Security

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

MEAN stack instead:
Angular 15 + Node + MongoDB: Login and Registration example

Related Posts:
– JWT tutorial: In-depth Introduction to JSON Web Token
– Angular 15 + Node.js Express: File Upload example
– Node.js Express and MongoDB: Login and Registration example

Fullstack CRUD App:
– Angular 15 + Node Express + MySQL example
– Angular 15 + Node Express + PostgreSQL example
– Angular 15 + Node Express + MongoDB example

Run both projects in one place:
How to Integrate Angular with Node.js Restful Services


Contents

  • Angular 15 Node.js JWT Authentication example
    • Screenshots
    • Demo
  • Flow for Authentication and Authorization
  • Node.js Express for Backend
    • Overview
    • Technology
    • Project Structure
    • Implementation and Source Code
  • Angular 15 for Frontend
    • Overview
    • Technology
    • Project Structure
    • Implementation and Source Code
  • Further Reading
  • Source Code
  • Conclusion

Angular 15 Node.js JWT Authentication example

It will be a full stack, with Angular 15 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 15 Client App.

– Anyone can access a public page before logging in:

angular-15-node-js-login-registration-jwt-example-public-access

– New user registration:

angular-15-node-js-login-jwt-example-registration

Signup Successfully:

angular-15-node-js-login-jwt-example-registration-success

– Signup form validation:

angular-15-node-js-login-jwt-example-registration-form-validation

If you want to know more details about Form Validation, please visit:

  • Angular 15 Form Validation example (Reactive Forms)
  • Angular 15 Template Driven Form Validation example

– Login Page with legal account:

angular-15-node-js-jwt-authentication-example-login-page

Login Successfully:

angular-15-node-js-jwt-authentication-example-login-success

HttpOnly Cookie set by the Server:

angular-15-node-js-jwt-authentication-example-httponly-cookie

– Profile page (for successful Login):

angular-15-node-js-jwt-authentication-example-profile

– For Authorization (Moderator account login), the navigation bar will change by authorities:

angular-15-node-js-jwt-authentication-authorization-example

HttpOnly Cookie is sent automatically with HTTP Request:

angular-15-node-js-jwt-authentication-authorization-httponly-cookie

– Browser Local/Session Storage for storing user information:

angular-15-node-js-jwt-user-authentication-authorization-example

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

angular-15-node-js-jwt-authentication-authorization-failed

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 15 and HttpOnly Cookies.

Flow for Authentication and Authorization

The diagram shows flow for User Registration, User Login and Resource access process.

angular-15-node-js-jwt-auth-example-flow

It’s not too difficult to understand. We have 3 endpoints for authentication:

  • api/auth/signup for User Registration
  • api/auth/signin for User Login
  • api/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:

angular-15-node-js-jwt-authentication-authorization-example-backend

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.17.1
  • 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:

angular-15-node-js-jwt-auth-example-backend-project

– 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 15 for Frontend

Overview

Our Angular 15 App can be summarized in component diagram below:

angular-15-node-js-jwt-auth-example-frontend

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 15
  • RxJS 7
  • Angular CLI 15
  • Bootstrap 4

Project Structure

This is the folder structure of our Angular front-end project:

angular-15-node-js-jwt-auth-example-frontend-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 15 Client (with Github) in the post:
Angular 15 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 15 + Node Express + MySQL example
– Angular 15 + Node Express + PostgreSQL example
– Angular 15 + 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 15 Login, Registration (Authentication) and Role Based 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 15 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!

angular angular 15 authentication authorization express httponly cookie jwt login node.js registration security sequelize token based authentication

Post navigation

Angular 15 + Spring Boot: File upload/download example
Angular 15 + Node.js Express: File Upload example
Buy me a coffee

Follow us

  • Facebook
  • Youtube
  • Github

Tools

  • Json Formatter
  • .
  • Privacy Policy
  • Contact
  • About
DMCA.com Protection Status © 2019-2022 bezkoder.com
X