Login System in Node.js Express with MySQL

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello friends in this video tutorial we will developing a login system with node.js express and mysql database [Music] and we will create login page for node.js application so in this tutorial you will learn how to create a login system in node.js with mysql database and here we will use express framework so with the help of this tutorial you can step by step create login system in node.js using express framework with mysql database and you can also learn how to validate user login details in node application and login into system and lastly you can also learn how to log out from node application now let's start discussing this topic so this is our testing database and here we have one user login table with table column like user it user email and user password and under this table we have already inserted some user login details under this table and we will validate this login details under node.js login system after this we have go to command prompt and first go into node directory in which we want to create node application [Music] and here we have create one directory with name like login and then after we have goes into login directory so under this directory we will download and install node.js express application so first we want to install express generator so we have to run following command so this command will install express generator next we want to install express application so for this we have to run following command so this command will install express application and in the above command ajs is a template engine of the express so after running the above command you will get following instructions [Music] next we want to install dependencies so we have to run following command which will install nodejs express framework dependencies so this command will install node.js express application dependencies after this we have go to text editor and here we can see that express application directory structure now we want to first make database connection so first we need to download mysql module so here in command prompt we have to run following command so this command will download and install node express mysql module so here we can see that node express mysql module has been installed in our node express application now we have go to text editor and here we want to create database connection file so here we have create new javascript file in the root folder with file name like database.js and here we will use this file for make mysql database connection and under this file we have to write constant mysql variable is equal to require function and under this function we have to write mysql so by using this code it will include mysql module under this file after this we want to define configuration for creating mysql database connection so here we have to write constant connection variable is equal to [Music] mysql.createconnection function so this function will make mysql database connection and under this function we have to define mysql database configuration in the json format so first we have to define host for mysql database connection so in first key we have to write host and in value we have to write localhost next we want to define database name from which we want to connect our node application so for this in the key we have to write database and in value we have to write testing and after this we want to define user name of the mysql database so for this in the key we have to write user and in the value we have to write rule and lastly in mysql database configuration we want to define password of the mysql database so for this here we have to write password and in the value we have to write blank so after define this configuration it will try to make mysql database connection now we want to execute mysql database connection so for this we have to write connection dot connect function so this function has been used for creator mysql connection [Music] and under this function we have to write one callback function with error as parameter under this function we have to write if statement and under condition we have to write error variable so this condition will check if there is any error occur then it will execute if block of code and under this block we have to write throw error so it will display database connection error but suppose there is no any error occurs then it will execute else block of code and under this block we have to write console.log function and under this function we have to write mysql database is connected successfully lastly we want to publish this mysql database connection so for this here we have to write module dot exports is equal to connection variable [Music] so here our mysql database connection code is ready after this we have go to roots directory and open index javascript file under this file first we want to make mysql database connection so here we have to write database variable is equal to request function and under this function we have to write path of the database connection file which will make mysql database connection [Music] now for login login page so we have go to get root and here we can see that index template file has been load and this file we will use for display login page and after login into system we want to display welcome message also with logout button so first we want to download and install express session module [Music] so for this we have go to command prompt and run following command so it will download and install express session module now we want to configure session and under this node express application so for this we have to open app.javascript file and under this file we need to import express session module so here first we need to import express session module so for this here we have to write session variable is equal to require function and under this we have to write express session module name so this way we can import express session modules under this app.js file so here we have to write app.use function and under this function we have to write session function so this function will set up session in node application under this function we have write first option secret with value webs lesson so it will generate a random unique string key which is used to authenticate a session [Music] in second option we have write resave which is set to true [Music] in third option we have to write save uninitialized which is set to true so this way we can set up session in node application after this we have again go to index javascript file and here in get root and under render function we have to write session key with value request dot session so here we have store session data in session variable which we can access in index template file now we have opened use directory index template file and under this file we have removed this code and paste this code [Music] and here we can see that we have already included bootstrap library link this file we will use for display login page and if user is already login then login user can view welcome message with logout link for logout from system so for this here we have to write node code syntax and between this we have to write if statement and under condition we have to write dot user it variable so this condition will check if user is login into system then it will execute this if block of code and under this block we want to display welcome message with logout link so here we have right welcome message which will be display after user has been login into system and below this we want to make logout link so here we have to create anchor tag with hraf attribute is equal to slash logout and class is equal to button and button primary class and between anchor text we have to write log out so it will create log out link which will send get request to logout route but suppose above condition falls then it will execute else block of code and under this block we want to create login form and this block of code will execute if user is not login into system so here we have paste this bootstrap 5 card components code and for create login form here we have to write form tag with method as equal to post and in action attribute we have to write slash login so it will submit form data to login root under this form for get user email address details here we have to write input type is equal to email and name is equal to user email address and class is equal to form control [Music] after this forget user password details we have to create password field so here we have to write input type is equal to password and name is equal to user password [Music] now for submit form data here we have to write input type is equal to submit class is equal to button and button primary and value is equal to login so by click on this button this login form data will be submitted to login root so here our html login form is ready now we have go to roots directory index javascript file and here first we want to create login root so for this here we have to write router.post function with two parameter so in first parameter we have to write slash login and in second parameter we have to write callback function with three parameter like request response and next [Music] under this function first we want to store form data into local variable so for this here we have to write user email address variable is equal to request dot body dot user email address variable so here we have to store email field data in local variable after this we have to write user password variable is equal to request dot body dot user password field so here we have store password field data into local variable below this we have to write if statement and under condition we have to write user email address variable and user password variable so this condition will check if both variable has some value then it will execute if block of code but suppose above condition falls then it will execute else block of code and under this block we have to write respond dot send function and under this function we have to write error message like please enter email address and password details and after this we have to write respond.n function so this code will display error on the web page but suppose above condition true then it will execute if block of code and here we have to write query variable is equal to select star from user login where user email is equal to dollar sign with open and close bracket and between this we have to write user email address variable so this query will check user login details in mysql user login table now we want to execute this query so for this here we have to write database variable.query function with two parameter [Music] in first parameter we have to write query variable in which we have store select query and in second parameter we have to write callback function with two parameter like error and data variable this function will return query execution result which we can get from this data variable under this function we have to write if statement and under condition we have to write data dot length value is greater than 0 so suppose this condition falls [Music] that means user has entered wrong password and it will execute else block of code and under this block we have to write response dot send function and under this function we have to write error message like incorrect email address so it will display this error message on the web page but suppose user has entered correct email address then it will execute if block of code and under this block we have to write for loop and under condition we have to write count variable value is equal to zero count variable value is less than the value of data dot length and count variable value increment by one so by using this for loop it will page data from this data variable and under this loop we have to write if statement and under condition we have to write data with count index dot user password is equal to user password variable so this condition has been compared user entered password with password stored in mysql table suppose user has entered wrong password then it will execute else block of code and under this block we have to write response dot send function and under this function we have to write error message like incorrect password so this message will be displayed on the web page but suppose user entered correct password then it will execute if block of code and under this block we want to store user it table column data into session variable so the session variable data we will use for validate user login is login and not so for store data in session variable here we have to write request dot session dot user it variable is equal to data variable with count index dot user it table column name so here login user user if table column data has been stored in session variable [Music] after this we want to redirect web page to root roots so for this here we have to write response dot redirect function and under this function we have to write slash so it will display index template file on web page but now it will display welcome message with logout link now we want to create another route for handle logout request so for this here we have to write router variable.getfunction with two parameter in first parameter we have to write slash logout and in second parameter we have to write callback function with three parameter like request response and next variable under this function first we want to delete all session variable data so for this here we have to write request dot session dot destroy function so this code will delete a remove all session variable so after delete all session data now we want to redirect web page to this root file index root so for this here we have to write response dot redirect function and under this function we have to write slash [Music] so it will redirect web page index root of this root file and user will be log out from system and now it will view login page on the web page so here our code is ready now we have to check output in the browser so we have go to command prompt and goes into directory where we have to create login directory and after this we have run this command so it will start node server now in the browser we have type localhost double colon with 3000 port number so here on the web page we can see login page has been displayed this is because in index root file we have create login page now first we want to check validation so we have to directly click on the login button so after click on the login button here we can see error message like please enter email address and password details so we have again go to login page and now we have enter wrong enter email address and wrong password details [Music] and after this we have click on login button so after click on the login button so here we can see error message like incorrect email address this is because we have entered wrong email address now in login page we have enter correct email address like john smith there gmail.com and now we have enter wrong password details and after this we have click on login button so after click on the login button here we can see error message like incorrect password so here all validation working properly now we have to enter correct email address and password details so after enter correct email address and password details now we have click on login button so after click on the login button here we can see that welcome message has been displayed on web page and below it we can see logout link so once user has been login into system then it will see this content on the web page now we want to log out from this system so we have click on the logout link and after click on the logout link here we can see that user has been log out from system and again user has been redirect to login page so this is simple node.js login system so in this video tutorial we have learned how to build login system in node.js by using express framework with mysql database lastly thank you for watching this video and we will be meet in new video tutorial in node.js [Music] you
Info
Channel: Webslesson
Views: 43,272
Rating: undefined out of 5
Keywords: node.js login, node js login, node js authentication, express login logout, login system, login page, node.js express login, node js login system, login system using nodejs, login system with nodejs epxress, node js login system mysql, how to build user login system, node js user login, node js login form, node js login tutorial, node js authentication tutorial, node js user login tutorial, node js login page tutorial, login in node js with mysql, webslesson, node js logout
Id: e5yZKTADxfk
Channel Id: undefined
Length: 20min 7sec (1207 seconds)
Published: Mon Jun 13 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.