In this tutorial, we're gonna build a Node.js Express Login & Registration example that supports Token Based Authentication with JWT (JSONWebToken). You'll know:
- Appropriate Flow for User Signup & User Login with JWT Authentication
- Node.js Express Architecture with CORS, Authenticaton & Authorization middlewares & Sequelize
- How to configure Express routes to work with JWT
- How to define Data Models and association for Authentication and Authorization
- Way to use Sequelize to interact with MySQL Database
Full Article: https://bezkoder.com/node-js-jwt-authentication-mysql/
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.
There are three important parts of a JWT: Header, Payload, Signature. Together they are combined to a standard structure:
The Client typically attaches JWT in Authorization header with Bearer prefix:
Authorization: Bearer [header].[payload].[signature]
Or only in x-access-token header:
For more details, you can visit:
In-depth Introduction to JWT-JSON Web Token
We will build a Node.js Express application in that:
- User can signup new account, or login with username & password.
- By User's role (admin, moderator, user), we authorize the User to access resources
This is our Node.js application demo running with MySQL database and test Rest Apis with Postman.
These are APIs that we need to provide:
/api/auth/signupsignup new account
/api/auth/signinlogin an account
/api/test/allretrieve public content
/api/test/useraccess User's content
/api/test/modaccess Moderator's content
/api/test/adminaccess Admin's content
The diagram shows flow of User Registration, User Login and Authorization process.
A legal JWT must be added to HTTP x-access-token Header if Client accesses protected resources.
You can have an overview of our Node.js Express App with 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.
For more details, implementation and Github, please visit:
- Node.js Rest APIs example with Express, Sequelize & MySQL
- Node.js + MongoDB: User Authentication & Authorization with JWT
- Node.js + PostgreSQL: User Authentication & Authorization with JWT
Fullstack (JWT Authentication & Authorization example):
- Node.js Express + Vue.js
- Node.js Express + Angular 8
- Node.js Express + Angular 10
- Node.js Express + Angular 11
- Node.js Express + React