evan_tech1234
evan_tech1234

Reputation: 123

bcrypt compare with nodejs

So im getting this error Error: Illegal arguments: string, undefined and this is the code

    router.post('/loguser', async (req, res) => {
        const compareHashed = await bcrypt.compare(req.body.password)
        const user = await User.findOne({
            username: req.body.username,
            password: compareHashed
        })
        if (user) {
            console.log('user is found')
        }
        if (!user) {
            console.log('user is not found')
        }
    })

does anyone know how to fix this, I know its a problem with bcrypt

Upvotes: 3

Views: 1542

Answers (1)

Tony George
Tony George

Reputation: 126

Hi bro first we hashed password and stored in db right

retrieve that hashed password from db and compare it with actual password

for eg: if we trying to login with email and password

    User.find({email:req.body.email}).exec().then(result=>{

  if(result.length < 1){
    console.log('email not found')
  }else{
    bcrypt.compare(req.body.password,result[0].password,(err,result)=>{
      if(err){
        console.log('password not match')
      }
      if(result){
        console.log('password match')
      }
    })
  }
})

Upvotes: 2

Related Questions