Search code examples
node.jsjwtnestjsnestjs-jwt

Wrong nbf time when decoding JWT token in nestjs


in my app.module I Register JWT as :

    JwtModule.register({
      secret: `${process.env.JWT_SECRET}`,
      signOptions: {
      expiresIn: "24h" ,
      notBefore:  Math.floor(new Date().getTime() / 1000) ,
      
    },

but when i decode the token I get these timestamps:

 iat: 1699944419,
 nbf: 3399888838,
 exp: 1700030819

nbf times shows a wrong datetime ( Sunday, September 26, 2077 5:03:58 PM )


Solution

  • jsonwebtoken's notBefore field seems to be time relative to the current time. If you pass a numeric value (which you do), that value will be treated as seconds, which is why you're doubling the time at the moment. You can see here that the nbf gets assigned the current timestamp plus the notBefore option, which leads to the doubled time. Give a time relative to the current for what you want the notBefore to be, instead of an absolute time.