Created
February 10, 2022 19:27
-
-
Save afraz-khan/f804ee2352cd07d24bb8019b2034fb07 to your computer and use it in GitHub Desktop.
Generate an expired JWT in Nodejs.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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