通过报名表格,我在数据库中插入了姓名、密码和电子邮件。,密码加密,则成功。但是,我创建了一个登录表单,通过它我插入了相同的电子邮件和相同的密码,但是*** bcrypt。比较***只得到登录失败。
user.js
router.get('/login',(req,res)=>{
res.render('user/login')
});
router.get('/signup',(req,res)=>{
res.render('user/signup')
})
router.post('/signup',(req,res)=>{
userHelpers.doSignup(req.body).then((response)=>{
console.log(req.body);
})
});
router.post('/login',(req,res)=>{
userHelpers.doLogin(req.body)
console.log(req.body) //.then((response)=>{
// console.log(response);
// console.log(req.body);
// if(response.status){
// req.session.loggedIn=true
// req.session.user=response.user
// res.redirect('/')
// }else{
// // req.session.loginErr="Invalid username or password"
// res.redirect('/login')
// }
// });
});
// router.get('/logout',(req,res)=>{
// req.session.destroy()
// res.redirect('/')
// })
module.exports = router;
user-helpers.js
const bcrypt=require('bcrypt')
module.exports={
doSignup:(userData)=>{
return new Promise (async(resolve,reject)=>{
userData.password= salt= await bcrypt.genSalt(10);
await bcrypt.hash(userData.password,10,(err, password,hash) => {
if(err) throw (err);
else
password=hash
db.get().collection(collection.USER_COLLECTION).insertOne(userData).then((data)=>{
resolve(data.insertedId)
// console.log(userData.password)
// console.log(userData.email)
});
});
});
},
doLogin:(userData)=>{
return new Promise(async(resolve,reject)=>{
let loginStatus=false
let response={}
let user=await db.get().collection(collection.USER_COLLECTION).findOne({email:userData.email})
if(user){
bcrypt.compare(userData.password,user.password).then((status)=>{
if(status){
console.log("login success");
// response.user=user
// response.status=true
// resolve(response)
}else{
console.log('login failed');
// resolve({status:false})
}
})
}else{
console.log("login falied")
// resolve({status: false})
}
})
}
}
i think inuser-helper.jsbcrypt.compare有一些问题
在我的终端只得到登录失败
根据官方文档,您应该这样使用
doLogin: (userData) => {
return new Promise(async (resolve, reject) => {
let loginStatus = false
let response = {}
let user = await db.get().collection(collection.USER_COLLECTION).findOne({ email: userData.email })
if (user) {
bcrypt.compare(userData.password, user.password).then((error, status) => {
if (error) {
console.log('login failed');
// resolve({status: false})
} else {
if (!status) {
console.log('login failed');
// resolve({status:false})
} else {
console.log("login success");
// response.user=user
// response.status=true
// resolve(response)
}
}
})
} else {
console.log("login failed")
// resolve({status: false})
}
})
}
我的代码中有一些错误,我已经修复了
var objectId=require('mongodb').ObjectId
module.exports={
doSignup:(userData)=>{
return new Promise (async(resolve,reject)=>{
// userData.password= salt= await bcrypt.genSalt(10);
userData.password=await bcrypt.hash(userData.password,10)
db.get().collection(collection.USER_COLLECTION).insertOne(userData).then((data)=>{
resolve(data)
// console.log(userData.password)
// console.log(userData.email)
});
});
},
doLogin:(userData)=>{
return new Promise(async (resolve, reject)=> {
let loginStatus = false
let response = {}
let user=await db.get().collection(collection.USER_COLLECTION).findOne({ email:userData.email })
if (user) {
// check if password matches
bcrypt.compare(userData.password,user.password).then((status)=> {
console.log(user.password)
console.log(userData.password)
if(status){
console.log("login success");
// response.user=user
// response.status=true
// resolve(response)
}else{
console.log('login failed');
// resolve({status:false})
}
})
}else{
console.log("login falied")
// resolve({status: false})
}
})
}
}