User login and registration using nodejs and mysql with example

User login and registration using nodejs and mysql with example

User login and registration using nodejs and mysql with example

In this tutorial, I am going to create simple email and password login authentication and register a user using nodejs and mysql.

I will save simple password in mysql database but this is not good practice for security reason so in next tutorial, you will know the use of BCrypt module of Node.js to encrypt passwords.

This tutorial will explain only how to save a record in mysql table and how to check email exist or not in the table with given password.

To handle post parameters of Http request in Node.js, we use Body-Parser module.

Step1: Table and directory structure

In first step, create a "users" table in the database by running following command in phpmyadmin or in mysql shell :

  1. CREATE TABLE `users` (
  2. `id` int(11) NOT NULL AUTO_INCREMENT,
  3. `name` varchar(255) NOT NULL,
  4. `email` varchar(255) NOT NULL,
  5. `password` varchar(255) NOT NULL,
  6. `created_at` datetime NOT NULL,
  7. `updated_at` datetime NOT NULL,
  8. PRIMARY KEY (`id`)

Now i will have to setup directory structure in following way :

  1. ├── controllers
  2. │ └── authenticate-controller.js
  3. │ └── register-controller.js
  4. ├── node_modules
  5. ├── config.js
  6. ├── index.js
  7. └── package.json

In Node.js package.json file is used to install all the dependencies.

  "name": "login",
  "version": "1.0.0",
  "description": "login authentication",
  "main": "index.js",
  "dependencies": {
    "body-parser": "^1.17.1",
    "express": "^4.14.1",
    "jsonwebtoken": "^7.3.0",
    "mysql": "^2.13.0"
  "devDependencies": {},
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  "author": "",
  "license": "ISC"

Step 2: Config.js to configure database connectivity

In this step, i will create mysql connection to work with database.

  1. var mysql = require('mysql');
  2. var connection = mysql.createConnection({
  3. host : 'localhost',
  4. user : 'root',
  5. password : '',
  6. database : 'test'
  7. });
  8. connection.connect(function(err){
  9. if(!err) {
  10. console.log("Database is connected");
  11. } else {
  12. console.log("Error while connecting with database");
  13. }
  14. });
  15. module.exports = connection;
Step 3: index.js

Now start with index.js file that is entry point of the application.

  1. var express=require("express");
  2. var bodyParser=require('body-parser');
  3. var app = express();
  4. var authenticateController=require('./controllers/authenticate-controller');
  5. var registerController=require('./controllers/register-controller');
  6. app.use(bodyParser.urlencoded({extended:true}));
  7. app.use(bodyParser.json());
  8. /* route to handle login and registration */
  11. app.listen(8012);
Step 4: Create Register Controller

In this step, I will register a user in database so that i can login in the application with register user.

  1. var connection = require('./../config');
  2. module.exports.register=function(req,res){
  3.     var today = new Date();
  4.     var users={
  5.      "name",
  6.      "email",
  7.      "password":req.body.password,
  8.      "created_at":today,
  9.      "updated_at":today
  10.     }
  11.     connection.query('INSERT INTO users SET ?',users, function (error, results, fields) {
  12.      if (error) {
  13.         res.json({
  14.             status:false,
  15.             message:'there are some error with query'
  16.         })
  17.      }else{
  18.          res.json({
  19.             status:true,
  20.             data:results,
  21.             message:'user registered sucessfully'
  22.         })
  23.      }
  24.     });
  25. }

In above code, i run a simple insert query of mysql to save user details in database with created date using date function.

Step 5: Create Authenticate Controller

In this step, i will validate user credentials.

  1. var connection = require('./../config');
  2. module.exports.authenticate=function(req,res){
  3.     var;
  4.     var password=req.body.password;
  5.     connection.query('SELECT * FROM users WHERE email = ?',[email], function (error, results, fields) {
  6.      if (error) {
  7.          res.json({
  8.             status:false,
  9.             message:'there are some error with query'
  10.             })
  11.      }else{
  12.      if(results.length >0){
  13.          if(password==results[0].password){
  14.                 res.json({
  15.                     status:true,
  16.                     message:'successfully authenticated'
  17.                 })
  18.          }else{
  19.              res.json({
  20.                  status:false,
  21.          message:"Email and password does not match"
  22.          });
  23.          }
  25.      }
  26.      else{
  27.      res.json({
  28.          status:false,    
  29.      message:"Email does not exits"
  30. });
  31.      }
  32.      }
  33.     });
  34. }

In above code, i check first if email exists in the database and then check password.

Before going with testing with the help of postman or other tools, start server from the command prompt first :

node index.js

Click here to know how to store encrypted password using bcrypt module in Node.js

Hashing passwords with Bcrypt and node.js

Phone: (+91) 8800417876
Noida, 201301
okey metin2 pvp serverler metin2 pvp
ataşehir escort maltepe escort bostancı escort kadıköy escort maltepe escort ataşehir escort tuzla escort pendik escort ümraniye escort kartal escort
pendik escort
izmir escort izmir escort izmir escort izmir escort denizli escort antalya escort antalya escort antalya escort izmir escort izmir escort izmir escort izmir escort izmir escort
antalya escort akseki escort aksu escort alanya escort bayan antalya escort antalya grup escort antalya masöz antalya rus escort antalya sınırsız escort antalya travesti escort bdsm escort beldibi escort bayan belek escort bayan çallı escort bayan çamyuva escort bayan demre escort bayan elmalı escort bayan fethiye escort bayan finike escort bayan göynük escort bayan güllük escort bayan ışıklar escort bayan kaş escort bayan kemer escort bayan konyaaltı altınkum escort bayan konyaaltı arapsuyu escort bayan konyaaltı escort bayan konyaaltı liman escort korkuteli escort bayan kültür escort kundu escort bayan lara escort bayan manavgat escort bayan muratpaşa escort bayan örnekköy escort bayan rus escort bayan serik escort bayan side escort bayan suriyeli escort tekirova escort bayan üniversiteli escort alanya escort mahmutlar escort alanya escort mahmutlar escort alanya escort mahmutlar escort istanbul escort avrupa yakası escort şişli escort topkapı escort istanbul vip escort maslak escort merter escort aksaray escort arnavutköy escort ataköy escort bağcılar escort bahçelievler escort bakırköy escort beşitaş escort beylikdüzü escort beyoğlu escort halkalı escort istanbul sınırsız escort kağıthane escort mecidiyeköy escort nişantaşı escort escort istanbul ataşehir escort beykoz escort çekmeköy escort kadıköy escort maltepe escort pendik escort sancaktepe escort şile escort sultanbeyli escort tuzla escort ümraniye escort üsküdar escort
beylikdüzü escort istanbul escort şişli escort beylikdüzü escort istanbul escort
kocaeli escort izmit escort bayan gebze escort izmit sınırsız escort escort bursa tuzla escort aydınlı escort orhanlı escort bursa escort escort bayan istanbul escort istanbul sınırsız escort istanbul bayan escort izmit escort izmit escort kocaeli escort sisli escort mecidiyekoy escort bursa escort bayan bursa görükle escort maltepe escort goztepe escort yakacik-yeni-escort bursa sınırsız escort sisli escort bayan taksim escort bursa escort porno izle sikiş izle atasehir escort antalya escort
ankara escort marmaris escort bursa escort mersin escort antalya escort izmir escort samsun escort bodrum escort çeşme escort kuşadası escort adana escort eskişehir escort gaziantep escort esenyurt escort pendik escort tuzla escort fatih escort ümraniye escort mecidiyeköy escort kartal escort bahçelievler escort bahçeşehir escort ataşehir escort bakırköy escort nişantaşı escort avcılar escort büyükçekmece escort kurtkoy escort konya escort beşiktaş escort taksim escort beylikdüzü escort malatya escort sakarya escort şişli escort maltepe escort kadıköy escort ataköy escort bostancı escort şirinevler escort bağcılar escort üsküdar escort etiler escort başaksehir escort ortaköy escort kağıthane escort sultangazi escort halkalı escort çekmeköy escort şaşkınbakkal escort mamak escort sincan escort keçiören escort çankaya escort beypazarı escort akyurt escort pursaklar escort kahramankazan escort göztepe escort alsancak escort bornova escort buca escort karabaglar escort karşıyaka escort konak escort narlıdere escort izmit escort başiskele escort kandira escort körfez escort karamürsel escort dilovası escort gölcük escort gebze escort kocaeli escort kayseri escort elazığ escort denizli escort diyarbakir escort adana escort ankara escort izmir escort antalya escort gaziantep escort kayseri escort konya escort mersin escort denizli escort kocaeli escort diyarbakır escort alanya escort manavgat escort bursa escort eskişehir escort sakarya escort samsun escort malatya escort trabzon escort adıyaman escort afyon escort ağrı escort aksaray escort amasya escort ardahan escort artvin escort aydın escort balikesir escort bartın escort batman escort bayburt escort bilecik escort bingöl escort bitlis escort bolu escort burdur escort çanakkale escort çankırı escort çorum escort düzce escort edirne escort elazığ escort erzincan escort erzurum escort giresun escort gümüşhane escort hatay escort ığdır escort ısparta escort maraş escort karabük escort karaman escort kars escort kastamonu escort kilis escort kırıkkale escort kırklareli escort kırşehir escort kütahya escort manisa escort mardin escort muğla escort muş escort nevşehir escort niğde escort ordu escort osmaniye escort rize escort urfa escort siirt escort sinop escort şırnak escort sivas escort ordu escort tokat escort tunceli escort usak escort van escort yalova escort yozgat escort zonguldak escort kıbrıs escort
adana escort ankara escort bursa escort diyarbakir escort eskisehir escort kayseri escort kocaeli escort konya escort malatya escort maras escort mersin escort ordu escort sakarya escort samsun escort hatay escort mardin escort tekirdag escort antalya escort aydin escort nigde escort nevsehir escort elazig escort manisa escort osmaniye escort denizli escort mugla escort
bedava bahis
ankara escort - ankara escort
ataşehir escort ataşehir anadolu yakası escort ankara escort antalya escort antalya escort

flynta 5 mg

flynta 5 mg fiyat

orcafil 5 mg




combo 100 mg

degra 100 mg




lifta 20

vigrande 100 mg

lifta 20 mg

lifta 5 mg

sildegra 100 mg


flynta 20 mg

viagra 100 mg fiyat

orijinal viagra

viagra 100 mg

eczane viagra

viagra sipariş

viagra satın al

viagra nedir

viagra yorum

viagra fiyat

viagra fiyatı 2021


cialis fiyat

cialis 20 mg

cialis 100 mg

cialis nedir

cialis 5 mg fiyatı

cialis hap

cialis 5 mg

cialis 20 mg eczane fiyatı

cialis 100 mg fiyat

cialis satış

izmir escort
ataşehir escort
pendik aktar