我正在尝试为我正在使用的路由添加身份验证express-jwt我添加了这个中间件来保护创建后的路由。但是在测试时,我在邮递员中遇到了错误。错误TypeError: Cannot read property 'authorization' of undefined<br> at Object.getTokenFromHeaders [as getToken]这些是我的 express jwt 代码认证.jsimport jwt from 'express-jwt';const getTokenFromHeaders = (req) => { const { headers: { authorization } } = req; console.log(authorization); <----- in this log i am getting token if(authorization && authorization.split(' ')[0] === 'Token') { return authorization.split(' ')[1]; } return null;};const auth = { required: jwt({ secret: 'secret', userProperty: 'payload', getToken: getTokenFromHeaders, }), optional: jwt({ secret: 'secret', userProperty: 'payload', getToken: getTokenFromHeaders, credentialsRequired: false, }),};module.exports = auth;路由.jsroutes.post('/post', auth.required, postController.post);
添加回答
举报
0/150
提交
取消