Node js User Authentication using MySQL and Express JS
source link: https://www.js-tutorials.com/nodejs-tutorial/node-js-user-authentication-using-mysql-express-js/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Node js User Authentication using MySQL and Express JS
Node js is providing an event-driven I/O model that makes node js application lightweight and efficient.This node js tutorial help to create user authentication system using MySQL and express js. I am creating a simple web application using nodejs, MySQL, express and ejs. I am using ejs aka Embedded JavaScript module for template engine in node js example.
I have already covered Simple Hello Nodejs tutorial, Add, Edit and Delete Record using MySQL and nodejs and Simple Rest API example using nodejs and MySQL. I am not covering some basics of node js like how to create nodejs?, what is package.json
?, How to to install npm. I am assuming you guys aware about these things, now lets go new things of nodejs.
We will cover following functionality into this node js tutorial:
- How to create database and table into MySQL server
- How to add routing with node js application
- How to handle session in node js application
- ejs template engine integration with node js application
- How to use partial file in node js application
- Notification/Message integration in node js application
Checkout Other NodeJS tutorials,
Simple Example of Node js Authentication with MySQL
We will authenticate user using MySQL database.We will create GET and POST type HTTP request to show login and post login information to server.I am using Bootstrap CSS to create beautiful login and registration form.
I am using following files and folder
I am creating a folder ‘nodejs_auth_example’.This is our node js project name.
views folder: This folder will contain all ejs template files.
public folder: This folder will contain all external css and js files.
routes folder: This folder will contain all controller file.
app.php: This file will use to create nodejs application server and routes url.
node_modules folder: This folder will contain all node.js packages.
Create Database and Table Structure for User Authentication
I am using MySQL server for user authentication.You need to create 'test'
name database into mysql server.You can create database using GUI or from command line whatever you like, Lets create 'users'
table into 'test'
MySQL database using below script.
I am storing password as a string you can change into MD5
, I am creating this tutorial for laymen nodejs learner so haven’t use md5 or any cipher module.
Create Package.json file
We will create package.json
file for this node.js application into root path of node js application.This file define what libraries or modules will be use in this node js project.You need to add below code into package.json
file.
lets install node js module using npm command, now open cmd
window and go to path of your node application, like C:/test/nodejs_auth_example
and run below example.
c:/test/nodejs_auth_example> npm install
Above command will install all dependency node js modules into node_modules
folder.
Node js Session Management using express-session
We will use express-session
node js module to manage session into application.This is a Simple session middleware for Express application.You need to instantiate session module into app.js
file like below,
How to Set Value into Session in Node JS Application
To store session data into variable, we simply use the request property req.session
, which is store serialized data into JSON, You can store single object or nested objects.
How to Get Session Value in Node js Application
We again use req.session
property to get session value from session object, below code used to get userId
from session,
var userId = req.session.userId;
How to Destroy/Unset Session Value in Node js Application
You can delete session using destroy method, that unset the session value.
How to show message/notification using req-flash in node js application
We will use req-flash
node module to manage flash message.You can use this module to send validation message or success message or any other notification message from controller method to view. You don’t need any configuration into app.js
file.
How to set message into node js application
We will create a message variable and assign notification text into it.
Here we are sending message text to index.ejs
view file.
How to Show Message/Notification in node js Application
I am using ejs template module so i will show message like below,
How to User Partial file in Node js Application
Partial view is very important in project template layout, We will define template in partial file(header, footer and sidebar) and include into main layout file when it needed.
header.ejs partial file
we will create a new header.ejs
file into views folder and put below code into this file.
footer.ejs partial file
We will create a new footer.ejs
file into views folder and put below code into this file.
Create Node JS Application file
We will create a main entry file of nodejs application like app.js
file,This file will use to instantiate all module and create connection with MySQL. You need to add below code into /nodejs_auth_example/app.js
file.
as you can see, I am running node server on 8080
port, you can change as per your need.You can get more info about nodejs and mysql from my previous article.
Now run node app.js
from command line that will show blank line without any error that means your server is connected with mysql. You can check on browser using http://localhost:8080
url.
Login and Registration page in Nodejs application
We will add login and registration page into this nodejs application, we will create user.js
file for control all action of login and registration process. We will create ejs template file for sign-in and signup view into views folder.
Step 1: Create a new routes/user.js
file and store into routes folder.
Step 2: Create a get routes to load HTML template file in app.js
file.
Step 3: Create a new views/index.ejs
file into views folder and put below code.
Step 4: Create a new views/signup.ejs
file into views folder and put below code.
Step 5: Created a new file routes/index.js
file into routes folder and action methods into this file for load index.ejs
view file.
Step 6: Create an action method into user.js
file to load signup.ejs
file.
Above steps will create login and signup page for users, so when you will open http://localhost:8080
into browser, you will get sign-in and signup page.
Authenticate User using NodeJS and MySQL
We will create user authentication using mysql and express JS. We will take input username and password from end user and post data to node server using POST HTTP method, now we will match username and password from MySQL table using SQL query.
If the user match then we will redirect user to dashboard page otherwise send to login page with error message.I am using flash message module for validation message and session module for maintain session across nodejs application.
we will create post request to authenticate user using MySQL. We will follow following steps to validate user,
Step 1: Create a new POST HTTP type routes in app.js
file.
Step 2: Create a login method into user.js
file.
As you can see, I have used session to store user information into session and message module for notification.
How to Register User in NodeJS Application
Registration is very common process of any web application which has user management system.This step help to register user and store data into MySQL database.We have already created register template view file and now create an action method to post form data using express js.
we will create post request to register user using MySQL. We will follow following steps to validate user,
Step 1: Create a new POST HTTP type routes in app.js
file.
Step 2: Create a signup()
method into user.js
file.
I have fired insert mysql query to add record into mysql using express js, after successfully insert record we have redirected user to login page.
Create Dashboard page in nodejs application
We need to create dashboard page into this node js example.We have created login functionality so when user will authenticate, we will redirect to dashboard home page.
create routes into app.js
file.
app.get('/home/dashboard', user.dashboard);//call for dashboard page after login
We have created routes and assigned dashboard method with this url, now define dashboard action method into user.js
file.
We are rendering profile.ejs
file when user is logged-in if not we are redirected to login view.We will create new views/profile.ejs
file.
we have used header.ejs
and footer.ejs
partial file to add header and footer view file.
Conclusion:
We have created a simple nodejs application and install dependency modules.We have also added user login and registration functionality. We have done authentication using mysql, added partial file view render functionality with session management in node js application, to show message we use notification module to show flash message in nodejs project.
You can download source code and Demo from below link.
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK