Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

how to use compare password in node js

//required files
const express = require('express')
const router = express.Router();

//bcryptjs
const bcrypt = require('bcryptjs')

//User modal of mongoDB
const User = require('../../models/User')


//Post request for login
router.post('/', (req, res) => {
    //email and password
    const email = req.body.email
    const password = req.body.password

    //find user exist or not
    User.findOne({ email })
        .then(user => {
            //if user not exist than return status 400
            if (!user) return res.status(400).json({ msg: "User not exist" })

            //if user exist than compare password
            //password comes from the user
            //user.password comes from the database
            bcrypt.compare(password, user.password, (err, data) => {
                //if error than throw error
                if (err) throw err

                //if both match than you can do anything
                if (data) {
                    return res.status(200).json({ msg: "Login success" })
                } else {
                    return res.status(401).json({ msg: "Invalid credencial" })
                }

            })

        })

})

module.exports = router
Comment

PREVIOUS NEXT
Code Example
Javascript :: find class using jquery 
Javascript :: update angular 
Javascript :: js sum of array 
Javascript :: canvas umu 
Javascript :: get element by 
Javascript :: event.target parent 
Javascript :: how to check if a string is alphabetic in javascript 
Javascript :: how to reverse a string in javascript 
Javascript :: how to send static file in express 
Javascript :: how to run different node app on server different domains 
Javascript :: get largest number in array javascript 
Javascript :: how to import background image in inline css in react 
Javascript :: get youtube id from url javascript 
Javascript :: HashLocationStrategy 
Javascript :: node eventemitter emit error 
Javascript :: convertir seconde 
Javascript :: if select option disabled jquerz 
Javascript :: react cdn 
Javascript :: pymongo add json to collection 
Javascript :: how to get the first letter of a string in jquery 
Javascript :: clear input field react-hook-form 
Javascript :: react onclick with event 
Javascript :: mysql query node.js 
Javascript :: node uuid 
Javascript :: get result and write to file node 
Javascript :: format time in moment 
Javascript :: capitalize the string 
Javascript :: time calculator js 
Javascript :: check date format javascript 
Javascript :: iterata a array in js 
ADD CONTENT
Topic
Content
Source link
Name
9+1 =