Skip to content

Instantly share code, notes, and snippets.

@afraz-khan
Created February 10, 2022 19:27
Show Gist options
  • Save afraz-khan/f804ee2352cd07d24bb8019b2034fb07 to your computer and use it in GitHub Desktop.
Save afraz-khan/f804ee2352cd07d24bb8019b2034fb07 to your computer and use it in GitHub Desktop.
Generate an expired JWT in Nodejs.
import * as jwt from 'jsonwebtoken';
export class PastJWTGenerator {
protected jwtSecret: string = 'jwt-secret';
protected expiresIn: number = 60 * 60 * 3; // three hours
// secondsInPast: number of seconds to move back in the past from where token must be issued.
generate(claims: any, secondsInPast: number, secondsToExpireIn?: number) {
const data = {
...claims,
iat: Math.floor(Date.now() / 1000) - secondsInPast,
};
return jwt.sign(data, jwtSecret, {
expiresIn: secondsToExpireIn ? secondsToExpireIn : this.expiresIn,
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment