简体   繁体   English

如何从节点 api 中的获取请求中获取 JWT 令牌解码

[英]How to get JWT token decoded from get request in node api

I'm sending JWT tokens accross requests for authorization, however I can't seem to get the token decode each time.我正在跨授权请求发送 JWT 令牌,但是我似乎无法每次都获得令牌解码。 It works with one method but not the other.它适用于一种方法,但不适用于另一种方法。 The first snippet gives a "decoded" token result from the server side, however the second one doesn't.第一个片段从服务器端给出了一个“解码”的令牌结果,但第二个没有。

public async getAllUsers(req: Request, res: Response) {
    try {
      const payload = req["decoded"]; // gives the token decoded
      if (payload) {
        let users: ILoginResult = await UserData.getAllUsers(payload);
        res.status(users.status).send(users.result);
      }
    } catch (e) {
      res.status(500).send({ error: e.toString() });
    }
  }
  public async getAccountDetails(req: Request, res: Response) {
    try {
      const user = req["decoded"]; // always undefined
      let details: IDetails = await AccountData.getAccountDetails(name);
      res.status(200).send(details);
    } catch (e) {
      let err = e.toString();
      res.status(500).send({ error: err });
    }
  }

The request from postman are included a bearer token which is provided at login and used throughout other parts of the app.来自邮递员的请求包含一个bearer token ,该bearer token在登录时提供并在应用程序的其他部分中使用。 Not sure why it works in the one but not the other.不知道为什么它适用于一个而不是另一个。 Would really appreciate if someone could better explain what's going on here and/or provide tips, advice, suggestions.如果有人能更好地解释这里发生的事情和/或提供提示、建议和建议,我们将不胜感激。

edit - adding request details编辑 - 添加请求详细信息

get request to: http://localhost:5000/api/v1/account with a token: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyIjoiYWRtaW4iLCJpYXQiOjE1Nzc5OTUwMjUsImV4cCI6MTU3ODE2NzgyNSwiaXNzIjoiaHR0cDovL2xvY2FsaG9zdCJ9.--msLba1VPs4Nv_B9YL6fk2DFHkQCgiVvDJFPt_UnDk get请求: http://localhost:5000/api/v1/account的标记: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyIjoiYWRtaW4iLCJpYXQiOjE1Nzc5OTUwMjUsImV4cCI6MTU3ODE2NzgyNSwiaXNzIjoiaHR0cDovL2xvY2FsaG9zdCJ9.--msLba1VPs4Nv_B9YL6fk2DFHkQCgiVvDJFPt_UnDk

The decoded property was used in a tutorial I was following that seemed to be added from the server side but was poorly explained and I haven't found a good alternative/explanation.decoded属性在我遵循的教程中使用,该教程似乎是从服务器端添加的,但解释得很差,我还没有找到好的替代方案/解释。 I don't think it has any middleware either.我也不认为它有任何中间件。 Very much open to alt methods.对替代方法非常开放。

Thanks to the suggestions from the comments I was able to find a missing piece in the route that creates the decoded property which is being used here.感谢评论中的建议,我能够在创建此处使用的decoded属性的路径中找到缺失的部分。 By adding the middleware to the router the request works as expected:通过将中间件添加到路由器,请求按预期工作:

import express from "express";
import UserController from "../controllers/UserController";
import valid from "../utils/ValidateToken";

export default (router: express.Router) => {
  router
    .route("/users")
    .post(UserController.addUser)
    .get(valid.validateToken, UserController.getAllUsers);

  router.route("/login").post(UserController.loginUser);
  router.route("/account").get(valid.validateToken, UserController.getAccountDetails);
};

The valid.validateToken was missing which is the bit that generates the decoded object from the JWT being passed. valid.validateToken丢失了,它是从传递的 JWT 生成解码对象的位。 Moral of the story, always double check everything.故事的寓意,总是仔细检查一切。 Thanks to all who commented/answered!感谢所有评论/回答的人!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM