【发布时间】:2021-08-17 09:28:08
【问题描述】:
我设计了一个身份验证功能,在通过我的 POSTMAN 端点测试时可以完美运行。这是 Authorization 值正确时的样子:
这就是我更改 Authorization 值以故意使授权过程失败时的样子:
在授权码下方查找:
const jwt = require ('jsonwebtoken');
const authenticate = (req, res, next)=>{
try {
const token = req.headers.authorization.split(' ')[1]
const decode = jwt.verify(token, 'verySecretValue')
console.log('Authentication PASSED!');
next();
}
catch (error) {
res.json({
message: 'Authentication FAILED!'
})
}
}
module.exports = authenticate
然后,现在在下面找到我用来渲染的代码:
.
.
.
const authenticate = require('./authentication/authenticate.js');
.
.
.
.
app.get('/list', authenticate, async (req,res)=> {
let countyResult = await county();
let transId = await transactionId();
transModel.find({transIndustry: 'Pharmacy'}, (err, docs)=> {
if (!err)
{
res.render('list', {data : docs, countyName: countyResult, transId: transId});
}
else
{
// res.status(status).send(body);
}
})
});
但是,当我尝试通过浏览器访问上面的端点/链接/地址时,我收到以下错误消息:
{"message":"Authentication FAILED!"}
我觉得这是一个授权值问题,我不太清楚在通过res.render('list', {data : docs, countyName: countyResult, transId: transId}); 呈现list 页面时应该如何传递这个值。
期待您的帮助。
【问题讨论】:
-
是否在
catch块的authenticate中间件中使用res.render选项?