0

Recently I'm learning Node.js I tried to apply JWT in my app. But it seems that there's a tiny period of expiration date in the JWT token. How to set an expiration time in the JWT token that will not expire for a couple of days but rather for a moderate period?

UmmeHania
  • 1
  • 1
  • 1

2 Answers2

0

Here EXPIRES_IN = "90d" represents 90 days . If you want your jwt expire in hours use EXPIRES_IN = "24h". h - hours || d- days

const jwt = require('jsonwebtoken');
const EXPIRES_IN = `90d`;
const JWT_SECRET='my-ultralong-secreat';


 const createSendToken = (id) => {

    const token = jwt.sign({ id },JWT_SECRET,{
         expiresIn:EXPIRES_IN
     });

    res.status(statusCode).json({
        token 
    });
  }

sai krishna
  • 131
  • 5
  • The question is a duplicate as you can see in the comment above. Please don't answer duplicate questions, esp. if you don't have anything to tell, that wasn't already mentioned in the suggested duplicate target. – jps May 16 '22 at 06:08
0
**You can try it**

import { sign as jwtSign } from 'jsonwebtoken';
const access_token = jwtSign(
  { sub: userId},
  config.get('APP_SECRET'),
  {
    expiresIn: config.get('ACCESS_TOKEN_EXPIRATION'),
  },
);
Shuvro
  • 222
  • 3
  • 7
  • The question is a duplicate as you can see in the comment above. Please don't answer duplicate questions, esp. if you don't have anything to tell, that wasn't already mentioned in the suggested duplicate target. – jps May 16 '22 at 06:08