不幸的是,比较函数的结果总是错误的?即使发布了正确的数据.你认为这可能与bcrypt的compare函数有关吗?
注册
module.exports.signupPost = async (req, res) => {
const { email, password } = req.body;
//create a user with hashed password
try {
const newUser = await User.create({ email, password });
newUser.password = await bcrypt.hash(newUser.password, 12);
newUser.save();
res.status(200).json({ user: newUser._id });
} catch (err) {
errors = handlerErr(err);
res.status(400).json({ errors });
}
};
登录
module.exports.loginPost = async (req, res) => {
const { email, password } = req.body;
try {
const user = await User.findOne({ email });
if (!user) {
res.status(404).json({ email: "No user found" });
}
//if user exist check password is a match
const user = await User.findOne({ email });
if (!user) {
return res.status(404).json({ email: "No user found" });
}
try {
const match = await bcrypt.compare(
password.toString(),
user.password,
function (err, res) {
console.log(res);// returns false
}
);
} catch (err) {}
};
user schema
数据库用户架构
const userSchema = new mongoose.Schema({
email: {
type: String,
required: [true, "Please enter a email"],
unique: true,
lowercase: true,
},
password: {
type: String,
required: [true, "Please enter a password"],
lowercase: true,
},
});