Skip to main content

BezKoder

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

Angular 12, MongoDB: Login example with Node.js | MEAN stack

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

In this tutorial, we will learn how to build a MEAN stack (MongoDB + Node.js Express + Angular 12) Authentication & Authorization with Login & Registration example. The back-end server uses Node.js Express with jsonwebtoken for JWT Authentication & Authorization, Mongoose for interacting with MongoDB database. The front-end will be created with Angular 12 using HttpInterceptor and Router. We’ll also perform Form validation on UI.

Related Post: Angular 12 + Node.js Express + MongoDB example: CRUD App

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

Other versions:
– MEAN stack: Login & Registration with Angular 8 example
– MEAN stack: Login & Registration with Angular 10 example
– MEAN stack: Login & Registration with Angular 11 example
– MEAN stack: Login & Registration with Angular 13 example
– MEAN stack: Login & Registration with Angular 14 example
– MEAN stack: Login & Registration with Angular 15 example


Contents

  • JWT (JSON Web Token)
  • MEAN stack Login and Registration with Angular 12 example
  • Flow for User Registration and User Login
  • Back-end with Node.js Express & Mongoose ODM
    • Overview
    • Technology
    • Project Structure
    • Implementation
  • Front-end with Angular 12, HttpInterceptor and Router
    • Overview
    • Technology
    • Project Structure
    • Implementation
  • Further Reading
  • Source Code
  • Conclusion

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

MEAN stack Login and Registration with Angular 12 example

It will be a full stack, with Node.js Express + MongoDB for back-end and Angular 12 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)

The images below shows screenshots of our Angular 12 Client App.

– Anyone can access a public page before logging in:

angular-12-mongodb-login-example-registration-mean-stack-public-page

– A new User can signup:

angular-12-mongodb-login-example-registration-mean-stack-signup-page

– Registration Form validation:

angular-12-mongodb-login-example-registration-mean-stack-form-validation

For more details about Form Validation, please visit:
Angular 12 Form Validation example (Reactive Forms)

– Login with legal account:

angular-12-mongodb-login-example-registration-mean-stack-signin-page

– Profile page:

angular-12-mongodb-login-example-registration-mean-stack-profile-page

– UI for admin role:

angular-12-mongodb-login-example-registration-mean-stack-authentication-authorization

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

angular-12-mongodb-login-example-registration-mean-stack-unauthorized

Flow for User Registration and User Login

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

angular-12-mongodb-login-and-registration-example-mean-stack-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 & Mongoose ODM

Overview

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

angular-12-mongodb-login-example-registration-mean-stack-server-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

An error message will be sent as HTTP response to Client when the middlewares throw any error, .

Controllers interact with MongoDB Database via Mongoose library and send HTTP response (token, user information, data based on roles…) to Client.

Technology

  • Express 4.17.1
  • bcryptjs 2.4.3
  • jsonwebtoken 8.5.1
  • mongoose 5.9.1
  • MongoDB

Project Structure

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

angular-12-mongodb-login-and-registration-example-mean-stack-server-project-structure

– config

  • configure MongoDB connection
  • 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 Mongoose Models

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

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

Implementation

Because the instruction of this implementation will very long, so I write it in a separated post (with Github) that you can find here:
Node.js + MongoDB: JWT Authentication & Authorization example

Front-end with Angular 12, HttpInterceptor and Router

Overview

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

angular-12-mongodb-login-and-registration-example-mean-stack-client-components

– 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 12
– RxJS 6

Project Structure

The structure of Angular front-end project is simple:

angular-12-mongodb-login-and-registration-example-mean-stack-client-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 12 Application in the post:
Angular 12 JWT Authentication example with Web Api

Other versions:
– Angular 8 JWT Authentication example with Web Api (with Github)
– Angular 10 JWT Authentication example with Web Api (with Github)
– Angular 11 JWT Authentication example with Web Api (with Github)
– Angular 13 JWT Authentication example with Web Api (with Github)

Further Reading

  • https://sequelize.org/v5/
  • https://www.npmjs.com/package/jsonwebtoken
  • Angular HttpInterceptor
  • In-depth Introduction to JWT-JSON Web Token

Angular 12 + Node.js Express + MongoDB example: CRUD App

Other versions:
– MEAN stack: Login & Registration with Angular 8 example
– MEAN stack: Login & Registration with Angular 10 example
– MEAN stack: Login & Registration with Angular 11 example
– MEAN stack: Login & Registration with Angular 13 example
– MEAN stack: Login & Registration with Angular 14 example
– MEAN stack: Login & Registration with Angular 15 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 MEAN stack Login and Registration with Angular 12 and MongoDB example using JWT for Authentication and Authorization along with flow for signup/login actions.

We also take a look at Node.js Express server architecture for JWT Authentication using jsonwebtoken & Mongoose ODM, as well as Angular 12 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 (with Github):
– Back-end

– Front-end:

  • Using Angular 8
  • Using Angular 10
  • Using Angular 11
  • Using Angular 12
  • Using Angular 13

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

You will want to know how to run both projects in one place:
How to Integrate Angular with Node.js Restful Services

For more details about Form Validation, please visit:
Angular 12 Form Validation example (Reactive Forms)

Happy learning, see you again!

angular angular 12 authentication authorization express httpinterceptor jwt login mean stack mongodb mongoose node.js security token based authentication

Post navigation

Angular 11 JWT Refresh Token with Http Interceptor example
Angular 12 + Node.js Express + PostgreSQL example: CRUD App

Follow us

  • Facebook
  • Youtube
  • Github

Tools

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