为什么 bcrypt.compareSync() 总是 return false?
Why bcrypt.compareSync() always return false?
我尝试在用户更改配置文件之前检查两个密码是否一致。
第一个在用户注册时保存在数据库中是这样的:
bcrypt.hashSync(req.body.pass, 8)
而第二个是用户作为请求发送的密码
这是我尝试比较的方式:
var passwordIsValid = bcrypt.compareSync(
req.body.pass,
user.pass // the result of searching the user in my database
);
但 passwordIsValid
始终为假,尽管字符串相同
这可能会有所帮助,这就是我将 bcrypt 与 mongoose 结合使用来散列我的密码并进行比较的方式:
/* eslint-disable import/prefer-default-export */
import mongoose from 'mongoose';
import {
hash as _hash,
compareSync
} from 'bcrypt-nodejs';
import mongooseDelete from 'mongoose-delete';
const {
Schema
} = mongoose;
const UserSchema = new Schema({
firstName: {
type: String,
required: true,
},
lastName: {
type: String,
required: true,
},
gender: {
type: String,
enum: ['male', 'female'],
},
profilePicture: {
type: String
},
password: {
type: String,
required: true,
},
email: {
type: String,
unique: true,
required: true,
},
}, {
timestamps: true
}, );
UserSchema.plugin(mongooseDelete);
// hash the password before the user is saved
UserSchema.pre('save', function hashPassword(next) {
// hash the password only if the password has been changed or user is new
if (!this.isModified('password')) {
next();
return;
}
// generate the hash
_hash(this.password, null, null, (err, hash) => {
if (err) {
next(err);
return;
}
// change the password to the hashed version
this.password = hash;
next();
});
});
// method to compare a given password with the database hash
UserSchema.methods.comparePassword = function comparePassword(password) {
const data = compareSync(password, this.password);
return data;
};
export {
UserSchema,
};
希望对您有所帮助:)
我尝试在用户更改配置文件之前检查两个密码是否一致。
第一个在用户注册时保存在数据库中是这样的:
bcrypt.hashSync(req.body.pass, 8)
而第二个是用户作为请求发送的密码
这是我尝试比较的方式:
var passwordIsValid = bcrypt.compareSync(
req.body.pass,
user.pass // the result of searching the user in my database
);
但 passwordIsValid
始终为假,尽管字符串相同
这可能会有所帮助,这就是我将 bcrypt 与 mongoose 结合使用来散列我的密码并进行比较的方式:
/* eslint-disable import/prefer-default-export */
import mongoose from 'mongoose';
import {
hash as _hash,
compareSync
} from 'bcrypt-nodejs';
import mongooseDelete from 'mongoose-delete';
const {
Schema
} = mongoose;
const UserSchema = new Schema({
firstName: {
type: String,
required: true,
},
lastName: {
type: String,
required: true,
},
gender: {
type: String,
enum: ['male', 'female'],
},
profilePicture: {
type: String
},
password: {
type: String,
required: true,
},
email: {
type: String,
unique: true,
required: true,
},
}, {
timestamps: true
}, );
UserSchema.plugin(mongooseDelete);
// hash the password before the user is saved
UserSchema.pre('save', function hashPassword(next) {
// hash the password only if the password has been changed or user is new
if (!this.isModified('password')) {
next();
return;
}
// generate the hash
_hash(this.password, null, null, (err, hash) => {
if (err) {
next(err);
return;
}
// change the password to the hashed version
this.password = hash;
next();
});
});
// method to compare a given password with the database hash
UserSchema.methods.comparePassword = function comparePassword(password) {
const data = compareSync(password, this.password);
return data;
};
export {
UserSchema,
};
希望对您有所帮助:)