我使用 jsonwebtoken 进行身份验证,但遇到问题:当我解码令牌时,出现错误:Property'id |设备ID| exp' 不存在于类型“string|JwtPayload”上。请帮我解决这个问题吗?非常感谢大家! 这是我的generateToken函数和decode函数
const payload = {
id : userId,
deviceId
}
public generateToken(payload : object, secretKey : string){
return jwt.sign({...payload, exp : Math.floor(Date.now()/ 1000 + EXP_2_MIN)}, secretKey)
}
public static decode(token : string ) {
return jwt.decode(token);
}
这是我正在尝试解码令牌:
async getSecretKey (request : Request) {
const headers = request.headers
const payload = JwtStrategy.decode(headers['authorization'].split(" ")[1])
const {id , deviceId, exp} = payload
我期望令牌解码成像我的有效负载对象这样的对象
您可以扩展类型
JwtPayload
并将其用作有效负载的类型。
interface ExtendedJwt extends JwtPayload {
// your custom properties
// adjust the types as well
id: string;
deviceId: string;
exp: string;
}
然后定义您的有效负载的类型。
public generateToken(payload: ExtendedJwt, secretKey: string) {
//...
}
您可能还想调整扩展负载的属性类型。