自定义网络令牌

时间:2019-05-19 13:46:19

标签: express jwt

当我在邮递员中输入我的有效电子邮件和密码时,我使用此自定义中间件进行Web令牌加密,它显示我错误,请先登录

const jwt = require(jsonwebtoken) const Profile = require({../models/profile

const auth =异步(req,res,next)=> {     尝试{         const token = req.header(Authorization)。replace(Bearer,``)         const encoded_token = jwt.verify(token,thisisMySecretKey)         const profile = await profile.findOne({_ id:encoded_token._id,'tokens.token':token})

    if(!profile) {
        throw new Error()
    }
    req.token = token
    req.profile = profile

    next()
} catch (e) {
    res.status(401).send({ error: `Please Login First`})
}

}

module.exports =身份验证

(节点:8924)UnhandledPromiseRejectionWarning:错误[ERR_HTTP_HEADERS_SENT]:将标头发送到客户端后无法设置标头     在ServerResponse.setHeader(_http_outgoing.js:470:11)     在ServerResponse.header(c:\ Users \ Mujahid Haq \ Desktop \ todo-routes \ node_modules \ express \ lib \ response.js:767:10)     在ServerResponse.send(c:\ Users \ Mujahid Haq \ Desktop \ todo-routes \ node_modules \ express \ lib \ response.js:170:12)     在ServerResponse.json(c:\ Users \ Mujahid Haq \ Desktop \ todo-routes \ node_modules \ express \ lib \ response.js:267:15)     在ServerResponse.send(c:\ Users \ Mujahid Haq \ Desktop \ todo-routes \ node_modules \ express \ lib \ response.js:158:21)     在routes.post(c:\ Users \ Mujahid Haq \ Desktop \ todo-routes \ routes \ profile-routes.js:27:25)     在process._tickCallback(内部/进程/next_tick.js:68:7) (节点:8924)UnhandledPromiseRejectionWarning:未处理的承诺拒绝。导致此错误的原因之一 通过在没有catch块的情况下抛出异步函数,或拒绝未使用.catch()处理的Promise。 (拒绝ID:1) (节点:8924)[DEP0018] DeprecationWarning:已弃用未处理的承诺拒绝。将来,未处理的承诺拒绝将以非零退出代码终止Node.js进程。

0 个答案:

没有答案