首页 > 解决方案 > JWT 令牌未定义

问题描述

在我尝试获取 JWT 令牌时,它总是说未定义。在邮递员中它的工作,但是当我通过客户端发送它给我一个错误。

const jwt=require('jsonwebtoken');
const config=require('config');

module.exports=function(req,res,next){

     const token  = req.body.token || req.query.token || 
     req.headers['x-access-token']||req.header('x-auth-token');
     if(!token){
          res.status(401).send("not authorized user");
        }
     else {
        try {
        const decoded=jwt.verify(token,"my_secret");
        req.userData=decoded;
        next();
    }catch(ex){
        res.status(401).send("invalid token");
    }
   }
}

我在做什么错。如何在客户端获取此令牌并将其发送到服务器?

标签: javascriptnode.jsjwt

解决方案


推荐阅读