In this tutorial, we will learn how to build a full stack Node.js Express + Vue.js Authentication example. The back-end server uses Node.js Express with jsonwebtoken for JWT authentication and Sequelize for interacting with MySQL database & Authorization. The front-end will be created with Vue and Vuex. We’ll also use vee-validate
to perform Form validation and vue-fontawesome
for make our UI more comfortable to view.
Related Posts:
– Node.js + MongoDB: User Authentication & Authorization with JWT
– Node.js + PostgreSQL: User Authentication & Authorization with JWT
Fullstack CRUD App:
- Vue.js + Node.js + Express + MySQL
- Vue.js + Node.js + Express + PostgreSQL
- Vue.js + Node.js + Express + MongoDB
Contents
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.
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
Node.js Express Vue.js Authentication example
It will be a full stack, with Node.js Express for back-end and Vue.js 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)
Screenshots
The images below shows screenshots of our Vue.js App.
– Anyone can access a public page before logging in:
– A new User can signup:
– Now User can Login. If it’s succesful, App directs the User to Profile page:
– UI for admin role
– If a User who doesn’t have Admin role tries to access Admin/Moderator Board page:
Demo Video
This is full Node.js Express + Vue.js Authentication demo Video (with form validation, check signup username/email duplicates, test authorization with 3 roles: Admin, Moderator, User):
[coming soon on Youtube…]
Flow for User Registration and User Login
The diagram shows flow of User Registration, User Login and Authorization process.
We have 2 endpoints for authentication:
api/auth/signup
for User Registrationapi/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 & Sequelize
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
- jsonwebtoken 9.0.0
- Sequelize 6.32.1
- 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
- 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 Sequelize Models
- user.model.js
- role.model.js
– server.js: import and initialize neccesary modules and routes, listen for connections.
Implementation
You can find step by step to implement this Node.js App in the post:
Node.js – JWT Authentication & Authorization with JSONWebToken example
For using MongoDB:
Node.js + MongoDB: JWT Authentication & Authorization example
Or PostgreSQL:
Node.js + PostgreSQL: JWT Authentication & Authorization example
Front-end with Vue.js & Vuex
Overview
Our Vue.js App can be summarized in component diagram below:
Let me explain it right now.
– The App
component is a container with Router
. It gets app state from Vuex store/auth
. Then the navbar now can display based on the state. App
component also passes state to its child components.
– Login
& Register
components have form for submission data (with support of vee-validate
). We call Vuex store dispatch()
function to make login/register actions.
– Our Vuex actions call auth.service
methods which use axios
to make HTTP requests. We also store or get JWT from Browser Local Storage inside these methods.
– Home
component is public for all visitor.
– Profile
component get user
data from its parent component and display user information.
– BoardUser
, BoardModerator
, BoardAdmin
components will be displayed by Vuex state user.roles
. In these components, we use user.service
to get protected resources from API.
– user.service
uses auth-header()
helper function to add JWT to HTTP Authorization header. auth-header()
returns an object containing the JWT of the currently logged in user from Local Storage.
Technology
– vue: 2.6.10
– vue-router: 3.0.3
– vuex: 3.0.1
– axios: 0.19.0
– vee-validate: 2.2.15
– bootstrap: 4.3.1
– vue-fontawesome: 0.1.7
Vue 3 version at:
Vue 3 Authentication with JWT, Vuex, Axios and Vue Router
Project Structure
The structure of Vue front-end project is simple:
You can understand it properly without any explanation.
Implementation
You can find step by step to implement this Vue – Vuex App in the post:
Vue.js JWT Authentication with Vuex and Vue Router
Typescript version: Vue/Vuex Typescript: JWT Authentication example
Vue 3 version: Vue 3 Authentication with JWT, Vuex, Axios and Vue Router
Source Code
You can find Github source code for this tutorial at: Vue + Node.js Github.
Conclusion
Now we have an overview of Node.js Express + Vue.js Authentication example using JWT and Vuex along with flow for signup/login actions.
We also take a look at Node.js Express server architecture for JWT Authentication using jsonwebtoken & Sequelize, as well as Vue.js 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:
– Back-end
– Front-end:
The Vue Clients also works well with back-end in the post:
– Node.js + MongoDB: JWT Authentication & Authorization
– Node.js + PostgreSQL: JWT Authentication & Authorization
Run both Project on same server/port:
How to serve Vue App with Express
Happy learning, see you again!
Further Reading
- https://sequelize.org/v5/
- https://www.npmjs.com/package/jsonwebtoken
- Vuex Guide
- In-depth Introduction to JWT-JSON Web Token
Fullstack CRUD App:
- Vue.js + Node.js + Express + MySQL
- Vue.js + Node.js + Express + PostgreSQL
- Vue.js + Node.js + Express + MongoDB
Hello, your tutorial is very helpful. I was able to create User Registration as applied Refresh Code. May I ask, do you have a tutorial on how to reset password? Thank you.
Where is the link for the source ?
Hi, you can find Github source code in the tutorials (back-end, front-end) that I mention at Conclusion section.
Many thanks!
Hi Bezkoder,
Your work is amazing and really clear, thank you for this !
thank you bezkoder
thanks a lot mate
nice tutorial, I love you bezkoder!
Thanks a lot! Authentication is always hard and I found this full stack tutorial.
Thanks! Your source code works well!
Thank you for sharing this. As far as I’ve checked this is the best tutorial for a free authentication solution and fairly simple to implement.
Hi,
Thanks for sharing. But I believe “refersh_token” for the JWT is required to keep the user logged in.
Can you please share where that can implemented? Interceptor maybe?
I’am an newbie and not figured out how to deploy both on my stack (xampp).
the program should work on a LAN.
Is it possible to have instructions on how to do it?
Thank you for a good example. I admire you!