最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to import TokenExpiredError thrown by jsonwebtoken's verify? - Stack Overflow

programmeradmin1浏览0评论

I'd like to check whether the type of an Error is TokenExpiredError throws by the jwt.verify function of the jsonwebtoken library using Typescript's instanceof, e.g.

import jwt from "jsonwebtoken";

function someFunction() {
    try {
        return jwt.verify(token, key);
    }catch(err) {
        if(err instanceof TokenExpiredError) {
            return attemptRenewal()
        }
        throw err
    }
}

How can I import the symbol TokenExpiredError?

I don't find any documentation about this important class and the only intuitive that comes to my mind

import { jwt, TokenExpiredError } from "jsonwebtoken";

causes jwt to be undefined.

I'm aware of workaround like performing string comparison with the class name, but I'd like to produce clean code.

I'm using jsonwebtoken 8.5.1.

I'd like to check whether the type of an Error is TokenExpiredError throws by the jwt.verify function of the jsonwebtoken library using Typescript's instanceof, e.g.

import jwt from "jsonwebtoken";

function someFunction() {
    try {
        return jwt.verify(token, key);
    }catch(err) {
        if(err instanceof TokenExpiredError) {
            return attemptRenewal()
        }
        throw err
    }
}

How can I import the symbol TokenExpiredError?

I don't find any documentation about this important class and the only intuitive that comes to my mind

import { jwt, TokenExpiredError } from "jsonwebtoken";

causes jwt to be undefined.

I'm aware of workaround like performing string comparison with the class name, but I'd like to produce clean code.

I'm using jsonwebtoken 8.5.1.

Share Improve this question edited May 24, 2019 at 21:41 Matthias 15.4k9 gold badges48 silver badges64 bronze badges asked May 24, 2019 at 15:18 Kalle RichterKalle Richter 8,72829 gold badges92 silver badges203 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 10

i had the same inquiry, but after a quick search and test, below should work

import * as jwt from "jsonwebtoken";

function someFunction() {
    try {
        return jwt.verify(token, key);
    }catch(err) {
        if(err instanceof jwt.TokenExpiredError) {
            return attemptRenewal()
        }
        throw err
    }
}

TokenExpiredError has been already imported as part of the jwt.

jsonwebtoken export a default object like: source

module.exports = {
  decode: require('./decode'),
  verify: require('./verify'),
  sign: require('./sign'),
  JsonWebTokenError: require('./lib/JsonWebTokenError'),
  NotBeforeError: require('./lib/NotBeforeError'),
  TokenExpiredError: require('./lib/TokenExpiredError'),
};

This mean, when you use import jwt from "jsonwebtoken"; syntax, jwt will come to a object with all properties of default export.

You can not use import { jwt, TokenExpiredError } from "jsonwebtoken";, because in default export object, we do not have jwt property,

If you want to import TokenExpiredError and the default object, you can follow a syntax like: import jwt, {TokenExpiredError} from "jsonwebtoken"; , then jwt still is default export object, and you have TokenExpiredError object (jwt.TokenExpiredError and TokenExpiredError is the same).

If you just want to use verify function and TokenExpiredError, the import line will come to: import {TokenExpiredError, verify} from "jsonwebtoken";, then you function will be like:

import {TokenExpiredError, verify} from "jsonwebtoken";

function someFunction() {
  try {
    return verify(token, key);
  } catch (err) {
    if (err instanceof TokenExpiredError) {
      return attemptRenewal()
    }
    throw err
  }
}
import jwt, {TokenExpiredError} from "jsonwebtoken";
发布评论

评论列表(0)

  1. 暂无评论