'how to set expire time of jwt token in node js? [duplicate]

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?



Solution 1:[1]

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 
    });
  }

Solution 2:[2]

**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'),
  },
);

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 sai krishna
Solution 2 Shuvro