gpt4 book ai didi

node.js - 为每个公司管理员创建路由登录

转载 作者:太空宇宙 更新时间:2023-11-04 01:30:37 26 4
gpt4 key购买 nike

我正在尝试为每个公司管理员的个人资料创建登录路由 url。

早些时候,当只有单一模式时,我尝试过使用相同的方法。现在我在嵌套结构中尝试相同的方法,发现有点困难。

现在我希望我的路由网址如下所示:- router.post('/:compId/admin/login')

我的想法很少,因为为每个文档创建路由对我来说是新的。

我的 Controller :-

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: []
};

这是我在路由登录中尝试的:-

router.post('/:compId/admin/login' , (req, res, next) => {

Admin.find({'admins.email': req.body.email},{ 'admins.companyId': req.params.compId })
.exec()
.then(admin => {
if(admin.admins.length < 1) {
return res.status(401).json({
message: "Auth failed. admin not found."
})
}
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 = "rgg@xyz.com"; //assaigning a user to admin
const role1 = 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,
role1,
comID: admin.admins[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);
});
});

还有我的回复数据:-

[{
"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
}]

问题

  • 那么如何为每个公司管理员的router.post('/:compId/admin/login')创建login,以便只有相应公司的对象登录时可以传入 JWT 详细信息吗?我添加的登录路径不起作用。

示例:- 假设第二家公司路线为 ('/155165/admin/login')

最佳答案

似乎您使用了错误的查询companyID不是admins的子级

所以查询应该是

Admin.find({'admins.email': req.body.email},{ 'companyID': req.params.compId })

关于node.js - 为每个公司管理员创建路由登录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56255998/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com