-
-
Notifications
You must be signed in to change notification settings - Fork 279
Closed
Description
userSchema.statics.findByCredentials = async (email, password) => {
const user = await User.findOne({ email })
if (!user) {
throw new Error('no user found!')
}
const isMatch = await bcrypt.compare(password, user.password)
if (!isMatch) {
throw new Error('invalid credentials!')
}
return user
}
The above code is matching the hash password that is stored in the database with a plain text password but it always returns false
.
so I tried this & it's working, could anyone explain to me; why this code is working & the above one is not working?
const bcrypt = require('bcryptjs')
const encription = async () => {
const password = 'abc1234';
console.log('plain password: ', password);
const hashPass = await bcrypt.hash(password, 8);
console.log(`hash password: ${hashPass}`);
const isValidPass = await bcrypt.compare(password, hashPass);
console.log(`is valid: ${isValidPass}`);
}
encription();
Metadata
Metadata
Assignees
Labels
No labels