node.js - 未使用 jwt 登录以返回身份验证失败
问题描述
自从我试图解决这个问题以来已经有几个小时了,但无法找到解决方案。
我创建了login
route ,我想在其中传递用户/管理员所在的整个对象的详细信息。不知何故,它是嵌套结构,所以我无法弄清楚。我可以用单一的模式结构来做到这一点。
登录路径:-
router.post('/:compId/admin/login' , (req, res, next) => {
Admin.find({ 'admins.email': req.body.email} , {companyID: req.params.compId})
.exec()
.then(admin => {
if(admin.admins.length < 1) {
return res.status(401).json({
message: "Auth failed. admin not found."
})
}
else{
console.log("nothing happened");
}
bcryptt.compare(req.body.admins.password, admin.admins[0].password, (err, result) =>{
if (err) {
return res.json({
message: "Auth failed. Check email and password"
});
}
if (result && admin.admins[0].verified === "true"){
const adminEmaill = "ggh@xyz.com"; //assaigning a user to admin
const role2 = admin.admins[0].email===adminEmaill? "superadmin" : "admin"; //check user id as admin or user
const token = jwt.sign(
{
email: admin.admins[0].email,
phoneNo: admin.admins[0].phoneNumber,
role2,
comID: admin[0].companyID
},
process.env.JWT_KEY,
{
expiresIn : "1h"
});
return res.status(200).json({
message: "Auth Successful",
token : token
});
}
else{
console.log("admin is not verified");
return res.json({
message: "Admin is not verified"
});
}
});
})
.catch(err =>{
if (err.code == 500)
res.status(500).send(["Something went wrong in login"]);
else
return next(err);
});
});
我的控制器:-
var admin = new Admin();
admin.companyName = req.body.companyName;
admin.address = req.body.address;
admin.contactDetails = req.body.contactDetails;
admin.admins = {
email : req.body.email,
password: req.body.password,
firstName : req.body.firstName,
lastName : req.body.lastName,
phoneNumber : req.body.phoneNumber,
designation : req.body.designation,
role : "admin",
verified :"false",
users: []
};
这是我的文件:-
[{
"admins": {
"email": "angjun.34@test-mail.info",
"password": "$2a$10$QgCJ4IaYXZK9JZIkLv2X9O/wnFpn0LEhFQujBco0M0TF2.X7OgDmW",
"firstName": "hdsdsds",
"lastName": "Ghodsdsdsh",
"phoneNumber": "4544343",
"designation": "Software Engineer",
"role": "admin",
"verified": "false",
"users": [],
"emailResetTokenn": "247c6e6794d15a311670da0bb13a4a8bf773b0e7f7b5dde0e555f421e2aef22f",
"emailExpires": "2019-05-22T15:05:43.974Z",
"saltSecret": "$2a$10$QgCJ4IaYXZK9JZIkLv2X9O"
},
"_id": "5ce510e7aca42c4c74fd9085",
"companyName": "TEST",
"address": "UAE",
"contactDetails": "54534454",
"companyID": "1223365",
"__v": 0
},
{
"admins": {
"email": "groham.224@test-mail.info",
"password": "$2a$10$QgCJ9O/wnFpn0LEhFco0M0TF2.X7OgDmW",
"firstName": "hdsdsds",
"lastName": "Ghodsdsdsh",
"phoneNumber": "4544343",
"designation": "Software Engineer",
"role": "admin",
"verified": "false",
"users": [],
"emailResetTokenn": "247c6e6794d15a311670da0bb13a4a8bf773b0e7f7b5dde0e555f421e2aef22f",
"emailExpires": "2019-05-22T15:05:43.974Z",
"saltSecret": "$2a$10$QgCJ4IaYXZK9JZIkLv2X9O"
},
"_id": "5ce510e7aca42c4c74fd9085",
"companyName": "RESTFUL Pvt Ltd",
"address": "UK",
"contactDetails": "54534454",
"companyID": "155165",
"__v": 0
}]
每个公司管理员只能传递他/她的整个公司对象。
我应该进行哪些更改才能在此处返回 JWT 令牌?到目前为止,我还没有联系服务器。
是因为我的管理员
if(admin.admins.length < 1)
不在数组中吗?
编辑:-通过更改 url 路由尝试此操作,但在此处获取身份验证失败响应。
bcryptt.compare(req.body.password, admin.admins.password, (err, result) =>{
if (err) {
return res.json({
message: "Auth failed. Check email and password"
});
}
解决方案
如果没有有关您遇到什么错误的更多信息,调试起来并不容易。我看到的一个潜在问题是这一行:
if (result && admin.admins[0].verified === "true"){
如果admins.verified
您的文档的属性是布尔值,那么上述行将始终解析为 false 并回退到该else
子句。因此,如果是这种情况,请删除引号:
if (result && admin.admins[0].verified === true){
推荐阅读
- java - 错误:找不到 com.google.android.gms.internal.zzbej 无法访问 zzbej 类文件
- mysql - phpmyadmin 需要一个符号名称!(靠近位置 312 的“)”)
- javascript - Android WebView Canvas 随机无法使用不同的宽度和高度
- jquery - 从 ajax 回发到控制器操作不起作用?
- bootstrap-4 - 始终为移动设备扩展垂直引导导航栏
- php - 为 laravel SPA 生成动态 csrf-token
- c# - RDLC C# RowNumber 小数点
- mysql - 如果左联接表不为空,则为空结果集,否则为空
- java - 在 Spring 和 Hibernate 5 中注册事件监听器
- html - 为什么标题不在正文边缘居中?