Checking and updating passwords with passport-local and bcrypt in node.js

I get an error [Error: data and hash arguments required] when I try to verify an existing user password in my node application. The context is that I ask my user to verify their existing password before changing it on the user profile page. My stack is node + mondodb (via mongoose) using passport-local with bcrypt.

Relevant Code:

 // code trying to match that returns the aforementioned error req.user.comparePassword(req.body.password, function (err, isMatch) { if (err) { return console.error(err); } if (isMatch) { console.log('passwords match'); // now save new password // Password verification userSchema.methods.comparePassword = function (candidatePassword, cb) { bcrypt.compare(candidatePassword, this.password, function (err, isMatch) { if (err) return cb(err); cb(null, isMatch); }); }; } } 

req.user refers to the current user object, and `req.body.password 'refers to the password received from the POST user. I use UserSchema, passport strategy and Bcrypt configuration from the local passport example here.

Can someone give instructions on how to verify that passwords match before upgrading?

+4
source share
1 answer

So, bcrypt.compare complains that one of the arguments is missing: data or hash . Thus, perhaps this.password returns null or undefined . Check your database entry for this user and make sure it has a valid hash.

+5
source

Source: https://habr.com/ru/post/1495048/


All Articles