Node.js Express Login example with MySQL database
Tags: node, javascript, security, webdev
Published: 2020-12-25 08:37:02

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/](https://bezkoder.com/node-js-jwt-authentication-mysql/)

## Token Based Authentication
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.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/osy8rwtr9y37vhmlp2za.png)

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 **Authorization** header with Bearer prefix:
```
Authorization: Bearer [header].[payload].[signature]
```
Or only 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](https://bezkoder.com/jwt-json-web-token/)

## Overview of Node.js Express Login & Registration example
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.

{% youtube fLcxkyMDW08 %}

These are APIs that we need to provide:

- POST 	`/api/auth/signup` 	signup new account
- POST 	`/api/auth/signin` 	login an account
- GET 	`/api/test/all` 	retrieve public content
- GET 	`/api/test/user` 	access User's content
- GET 	`/api/test/mod` 	access Moderator's content
- GET 	`/api/test/admin` 	access Admin's content

## Flow for Signup & Login with JWT Authentication
The diagram shows flow of User Registration, User Login and Authorization process.

![node-js-login-mysql-example-flow](https://dev-to-uploads.s3.amazonaws.com/i/crv22c186lcj9spryp44.png)

A legal JWT must be added to HTTP **x-access-token** Header if Client accesses protected resources.

You may need to implement Refresh Token like this:

![node-js-express-refresh-token-example-flow](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wohb3x8su8khbx4mppk5.png)

More details at: [https://bezkoder.com/jwt-refresh-token-node-js/](https://bezkoder.com/jwt-refresh-token-node-js/)

## Node.js Express Architecture with Authentication & Authorization
You can have an overview of our Node.js Express App with the diagram below: 

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/t90e850g7799rtvect0i.png)

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:
[https://bezkoder.com/node-js-jwt-authentication-mysql/](https://bezkoder.com/node-js-jwt-authentication-mysql/)

Using Cookies: [https://www.bezkoder.com/node-js-express-login-example/](https://www.bezkoder.com/node-js-express-login-example/)

Front-end that works well with this:
- [Angular 8](https://bezkoder.com/angular-jwt-authentication/)
- [Angular 10](https://bezkoder.com/angular-10-jwt-auth/)
- [Angular 11](https://bezkoder.com/angular-11-jwt-auth/)
- [Angular 12](https://bezkoder.com/angular-12-jwt-auth/)
- [Vue.js](https://bezkoder.com/jwt-vue-vuex-authentication/)
- [React (without Redux)](https://bezkoder.com/react-jwt-auth/)
- [React Redux](https://bezkoder.com/react-redux-jwt-auth/)

You may need to implement Refresh Token like this:

![node-js-express-refresh-token-example-flow](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wohb3x8su8khbx4mppk5.png)

More details at: [https://bezkoder.com/jwt-refresh-token-node-js/](https://bezkoder.com/jwt-refresh-token-node-js/)

## Further Reading

- [Node.js Rest APIs example with Express, Sequelize & MySQL](https://bezkoder.com/node-js-express-sequelize-mysql/)
- [Node.js + MongoDB: User Authentication & Authorization with JWT](https://bezkoder.com/node-js-mongodb-auth-jwt/)
- [Node.js + PostgreSQL: User Authentication & Authorization with JWT](https://bezkoder.com/node-js-jwt-authentication-postgresql/)

Fullstack:
<ul>
	<li><a href="https://bezkoder.com/vue-js-node-js-express-mysql-crud-example/">Vue.js + Node.js + Express + MySQL example</a></li>
	<li><a href="https://bezkoder.com/vue-node-express-mongodb-mevn-crud/">Vue.js + Node.js + Express + MongoDB example</a></li>
	<li><a href="https://www.bezkoder.com/angular-node-express-mysql/">Angular 8 + Node.js Express + MySQL</a></li>
	<li><a href="https://www.bezkoder.com/angular-node-express-postgresql/">Angular 8 + Node.js Express + PostgreSQL</a></li>
	<li><a href="https://www.bezkoder.com/angular-mongodb-node-express/">Angular 8 +  Node.js Express + MongoDB</a></li>
	<li><a href="https://www.bezkoder.com/node-js-express-angular-jwt-auth/">Node.js Express + Angular 8: User Authentication & Authorization example</a></li>
	<li><a href="https://www.bezkoder.com/angular-10-node-js-express-mysql/">Angular 10 + Node.js Express + MySQL example</a></li>
	<li><a href="https://www.bezkoder.com/angular-10-node-express-postgresql/">Angular 10 + Node.js Express + PostgreSQL example</a></li>
	<li><a href="https://www.bezkoder.com/angular-10-mongodb-node-express/">Angular 10 + Node.js Express + MongoDB</a></li>
	<li><a href="https://www.bezkoder.com/node-js-express-angular-10-jwt-auth/">Node.js Express + Angular 10: JWT Authentication & Authorization example</a></li>
	<li><a href="https://www.bezkoder.com/angular-11-node-js-express-mysql/">Angular 11 + Node.js Express + MySQL</a></li>
	<li><a href="https://www.bezkoder.com/angular-11-node-js-express-postgresql/">Angular 11 + Node.js Express + PostgreSQL</a></li>
	<li><a href="https://www.bezkoder.com/angular-11-mongodb-node-js-express/">Angular 11 + Node.js Express + MongoDB</a></li>
	<li><a href="https://www.bezkoder.com/node-js-angular-11-jwt-authentication/">Node.js Express + Angular 11: JWT Authentication & Authorization example</a></li>
	<li><a href="https://www.bezkoder.com/angular-12-node-js-express-mysql/">Angular 12 + Node.js Express + MySQL</a></li>
	<li><a href="https://www.bezkoder.com/angular-12-node-js-express-postgresql/">Angular 12 + Node.js Express + PostgreSQL</a></li>
	<li><a href="https://www.bezkoder.com/angular-12-mongodb-node-js-express/">Angular 12 + Node.js Express + MongoDB</a></li>
	<li><a href="https://www.bezkoder.com/node-js-angular-12-jwt-auth/">Node.js Express + Angular 12: JWT Authentication & Authorization example</a></li>
	<li><a href="https://www.bezkoder.com/angular-13-node-js-express-mysql/">Angular 13 + Node.js Express + MySQL</a></li>
	<li><a href="https://www.bezkoder.com/angular-13-node-js-express-postgresql/">Angular 13 + Node.js Express + PostgreSQL</a></li>
	<li><a href="https://www.bezkoder.com/mean-stack-crud-example-angular-13/">Angular 13 + Node.js Express + MongoDB</a></li>
	<li><a href="https://www.bezkoder.com/node-js-angular-13-jwt-auth/">Node.js Express + Angular 13: JWT Authentication & Authorization example</a></li>
	<li><a href="https://www.bezkoder.com/angular-14-node-js-express-mysql/">Angular 14 + Node.js Express + MySQL</a></li>
	<li><a href="https://www.bezkoder.com/angular-14-node-js-express-postgresql/">Angular 14 + Node.js Express + PostgreSQL</a></li>
	<li><a href="https://www.bezkoder.com/mean-stack-crud-example-angular-14/">Angular 14 + Node.js Express + MongoDB</a></li>
	<li><a href="https://www.bezkoder.com/node-js-angular-14-jwt-auth/">Node.js Express + Angular 14: JWT Authentication & Authorization example</a></li>
	<li><a href="https://bezkoder.com/react-node-express-mysql/">React + Node.js + Express + MySQL example</a></li>
</ul>

Deployment:
- <a href="https://bezkoder.com/deploy-node-js-app-heroku-cleardb-mysql/">Deploying/Hosting Node.js app on Heroku with MySQL database</a>
- [Dockerize Node.js Express and MySQL example – Docker Compose](https://www.bezkoder.com/docker-compose-nodejs-mysql/)

Node.js & MySQL Associations:
- <a href="https://bezkoder.com/sequelize-associate-one-to-many/">One-to-Many Relationship example</a>
- <a href="https://bezkoder.com/sequelize-associate-many-to-many/">Many-to-Many Relationship example</a>

Dockerize: [Docker Compose: Node.js Express and MySQL example](https://www.bezkoder.com/docker-compose-nodejs-mysql/)