【发布时间】:2021-08-26 02:07:12
【问题描述】:
我正在尝试进行 jwt 身份验证,但在验证功能上遇到了这样的错误。
没有重载匹配这个调用。 重载 1 of 3, '(token: string, secretOrPublicKey: Secret, options?: VerifyOptions | undefined): string |对象',给出以下错误。 'string | 类型的参数字符串[] | undefined' 不能分配给'string' 类型的参数。 类型“未定义”不可分配给类型“字符串”。 Overload 2 of 3, '(token: string, secretOrPublicKey: Secret | GetPublicKeyOrSecret, callback?: VerifyCallback | undefined): void',给出以下错误。
import { NextFunction, Request, Response } from "express";
import jwt from "jsonwebtoken";
import config from "../config/default"
var authorization = function (req:Request, res:Response, next:NextFunction) {
var token = req.headers['x-access-token'];
var msg = {auth: false, message: 'No token provided.'};
if (!token) res.status(500).send(msg);
jwt.verify(token, config.token.secret, function (err) {
var msg = {auth: false, message: 'Failed to authenticate token.'};
if (err) res.status(500).send(msg);
next();
});
}
module.exports = authorization;
【问题讨论】:
-
它即将到来是因为您的值
token或secret可能未定义。
标签: node.js typescript express jwt express-jwt