jsonwebtoken#JsonWebTokenError TypeScript Examples

The following examples show how to use jsonwebtoken#JsonWebTokenError. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Token.ts    From expresso with MIT License 6 votes vote down vote up
/**
 *
 * @param token
 * @returns
 */
function verifyAccessToken(token: string): DtoVerifyAccessToken {
  try {
    if (!token) {
      return { data: null, message: 'Unauthorized!' }
    }

    const data = jwt.verify(token, JWT_SECRET_ACCESS_TOKEN)
    return { data, message: 'Token is verify' }
  } catch (err) {
    if (err instanceof TokenExpiredError) {
      console.log(logErrServer('JWT Expired Error:', err.message))
      return { data: null, message: `JWT Expired Error: ${err.message}` }
    }

    if (err instanceof JsonWebTokenError) {
      console.log(logErrServer('JWT Token Error:', err.message))
      return { data: null, message: `JWT Token Error: ${err.message}` }
    }

    if (err instanceof NotBeforeError) {
      console.log(logErrServer('JWT Not Before Error:', err.message))
      return { data: null, message: `JWT Not Before Error: ${err.message}` }
    }
  }
}
Example #2
Source File: app.ts    From Designer-Server with GNU General Public License v3.0 5 votes vote down vote up
setupDbAndServer = async () => {
    let connectionInfo = {
      ...ormConfig.defaultConnection
    }

    if(process.env.NODE_ENV !== 'production') {
      connectionInfo = ormConfig.defaultConnection = {
        ...ormConfig.defaultConnection,
        database: "designer-test"
      }
    }

    await createConnection(connectionInfo)
    await createConnection(ormConfig.datasetConnection)

    await RedisManager.Instance.connect();

    RegisterRoutes(<express.Express>this.app);
    BullManager.setupBull(this.app);

    this.app.use("/api-docs", swaggerUi.serve, async (_req: express.Request, res: express.Response) => {
      return res.send(
        swaggerUi.generateHTML(swagger)
      );
    });

    this.app.use(function(req, res, next) {
      let err = new ApplicationError(404, 'Not Found');
      next(err);
    });

    this.app.use(function errorHandler(
      err: unknown,
      req: ExRequest,
      res: ExResponse,
      next: NextFunction
    ): ExResponse | void {
      console.log(err)

      if (err instanceof JsonWebTokenError) {
        return res.status(401).json({
          message: "Token expired"
        })
      }

      if (err instanceof ValidateError) {
        return res.status(422).json({
          message: "Validation Failed",
          details: err?.fields,
        });
      }

      if (err instanceof Error) {
        if(err.name === "ApplicationError") {
          const applicationError = <ApplicationError> err;
          return res.status(applicationError.statusCode).json({
            code: applicationError.message
          });
        }
      }

      return res.status(500).json({
        message: "Internal Server Error",
      });

      next();
    });

    await this.startServer();
    this.setupGrpcServer();
  }