ary*_*ing 9 jwt typescript json-web-token
我正在使用jsonwebtoken解码令牌,我正在尝试获取到期日期。Typescript 抛出有关该exp属性的错误,我不太确定如何解决它们:
import jwt from 'jsonwebtoken'
const tokenBase64 = 'ey...' /* some valid token */
const token = jwt.decode(tokenBase64)
const tokenExpirationDate = token.exp
// ^^^
// Property 'exp' does not exist on type 'string | object'. Property 'exp' does not exist on type 'string'.
Run Code Online (Sandbox Code Playgroud)
我已经安装@types/jsonwebtoken,并寻找要转换的令牌类型token,但没有找到。建议?
使用
@types/jsonwebtoken@7.2.3jsonwebtoken@8.1.0.tsconfig:
{
"compilerOptions": {
"allowJs": true,
"baseUrl": ".",
"jsx": "Preserve",
"moduleResolution": "Node",
"module": "ESNext",
"sourceMap": true,
"removeComments": true,
"allowSyntheticDefaultImports": true,
"target": "ESNext"
}
}
Run Code Online (Sandbox Code Playgroud)
JSG*_*uru 16
这就是我使用 TS 解码的方式
import jwt from 'jsonwebtoken';
export const isTokenExpired = (token: string): boolean => {
try {
const { exp } = jwt.decode(token) as {
exp: number;
};
const expirationDatetimeInSeconds = exp * 1000;
return Date.now() >= expirationDatetimeInSeconds;
} catch {
return true;
}
};
Run Code Online (Sandbox Code Playgroud)
不需要,但你也可以
import 'jest';
import jwt from 'jsonwebtoken';
import { isTokenExpired } from 'path-to-isTokenExpired/isTokenExpired';
describe('isTokenExpired', () => {
it('should return true if jwt token expired', () => {
const currentTimeInSecondsMinusThirtySeconds = Math.floor(Date.now() / 1000) - 30;
const expiredToken = jwt.sign({ foo: 'bar', exp: currentTimeInSecondsMinusThirtySeconds }, 'shhhhh');
expect(isTokenExpired(expiredToken)).toEqual(true);
});
it('should return false if jwt token not expired', () => {
const currentTimeInSecondsPlusThirtySeconds = Math.floor(Date.now() / 1000) + 30;
const notExpiredToken = jwt.sign({ foo: 'bar', exp: currentTimeInSecondsPlusThirtySeconds }, 'shhhhh');
expect(isTokenExpired(notExpiredToken)).toEqual(false);
});
it('should return true if jwt token invalid', () => {
expect(isTokenExpired('invalidtoken')).toEqual(true);
});
});
Run Code Online (Sandbox Code Playgroud)
当我使用import jwt from 'jsonwebtoken'
带有var jwt = require('jsonwebtoken');[1]的行时,我收到了相同的错误消息,但它工作正常:
var jwt = require('jsonwebtoken');
var tokenBase64 = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiZXhwIjoiMTUxMTk1MDcwMyIsImFkbWluIjp0cnVlfQ.wFC-9ZsqA9QuvLkRSkQmVYpUmgH9R-j8M4D0GECuPHY';
const token = jwt.decode(tokenBase64);
const tokenExpirationDate = token.exp
console.log(tokenExpirationDate);
Run Code Online (Sandbox Code Playgroud)
[1] 另见https://github.com/auth0/node-jsonwebtoken
我发现用户导入的唯一方法是:
import { sign, verify } from 'jsonwebtoken';
sign('Hello', 'secret');
Run Code Online (Sandbox Code Playgroud)
但我认为该require方法更好,因此您不必显式导入每个函数。
小智 5
从 jsonwebtoken 8.3 开始,jsonwebtoken.decode()具有以下类型定义:
export function decode(
token: string,
options?: DecodeOptions,
): null | { [key: string]: any } | string;
Run Code Online (Sandbox Code Playgroud)
由于 Typescript 无法推断正确的类型并且exp未知,因此最简单的方法是将结果强制转换为any.
import jwt from 'jsonwebtoken'
const tokenBase64 = 'ey...' /* some valid token */
const token: any = jwt.decode(tokenBase64)
const tokenExpirationDate = token.exp
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
17508 次 |
| 最近记录: |