javascript 无法读取未定义的属性(阅读“authorization”)

kse8i1jr  于 2023-04-04  发布在  Java
关注(0)|答案(1)|浏览(129)

这是我的密码

const bcrypt = require("bcryptjs");
const jwt = require("jsonwebtoken");
const userRepository = require("../repositories/userRepository");
const SALT = 10;

async whoami(req, next) {
    const bearerToken = req.headers.authorization;
    const token = bearerToken.split("Bearer ")[1];
    const tokenPayload = jwt.verify(token, process.env.JWT_SIGNATURE_KEY || "Rahasia");

    req.user = await userRepository.find(tokenPayload.id);
    next();
},

这是控制器

whoami(req, res) {
    authService
        .whoami(req.body)
        .then((user) => {
            res.status(200).json(user);
        })
        .catch((err) => {
            console.log(err);
            res.status(422).json({
                status: "FAIL",
                message: err.message,
            });
        });
},
xlpyo6sf

xlpyo6sf1#

//change whoami to look like this

whoami(req, res) {
    authService
        .whoami(req) //req instead of only request body
        .then((user) => {
            res.status(200).json(user);
        })
        .catch((err) => {
            console.log(err);
            res.status(422).json({
                status: "FAIL",
                message: err.message,
            });
        });
},

相关问题