how to verify jwt token in nodejs never expire

0 votes

In node js, I implemented a JWT token. I'm able to create a jwt token. Now I'd like to see when it will expire or become invalid. According to the documentation, it will expire after 120 milliseconds. However, my token will not expire. Why does it always decode the token?

This is how I make the token.

app.get("/saveData", async (req, res) => {
  try {
    const token = await userService.create({
      userId: "abcp",
      password: "hello",
      appsAccess: ["yes", "test"]
    });

    res.send(token);
  } catch (error) {
    console.log(error);
  }
});

and verify the token like this

app.get("/verify-token", async (req, res) => {
  let tokenStatus = await userService.verifyAccessToken(
    "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyIjp7InVzZXJJZCI6ImFiY3AiLCJhcHBzQWNjZXNzIjpbInllcyIsInRlc3QiXSwiX2lkIjoiNWQ3Yzk4MTYzZmQ1NGIwOGUwMjYzNjg0IiwiX192IjowfSwiaWF0IjoxNTY4NDQ2NDg2LCJpc3MiOiJqamoiLCJzdWIiOiJhYmNwIn0.1fqzYJ1p9jSIiNjbA7MwEsU4EsMmmpxF34TU1ZjonSA"
  );
  res.send(tokenStatus);
});

here is my code

verifyAccessToken(token) { return jwt.verify(token, "jhjhhj"); }

I want if i generate the token it will expire after 10min or 30min

Jun 16, 2022 in Node-js by Vaani
• 7,070 points
7,565 views

1 answer to this question.

0 votes

You can achieve this by using the expiresIn option. Based on your code, here's an example:

const token = jwt.sign(payload, "jhjhhj", {
  algorithm: "HS256",
  issuer: "jjj",
  subject: `${user.userId}`,
  expiresIn: "10m"
});
answered Jun 17, 2022 by Neha
• 9,020 points

Related Questions In Node-js

0 votes
1 answer

How to get the _id of inserted document in Mongo database in NodeJS?

Hello @kartik, A shorter way than using second ...READ MORE

answered Sep 7, 2020 in Node-js by Niroj
• 82,840 points
13,765 views
0 votes
1 answer

How to run app.js in nodejs?

Hello @kartik, Assuming I have node and npm properly installed on the ...READ MORE

answered Oct 13, 2020 in Node-js by Niroj
• 82,840 points
2,979 views
0 votes
1 answer

How to get path from the request in nodejs?

Hello @kartik, Try this out: var http = require('http'); var ...READ MORE

answered Oct 14, 2020 in Node-js by Niroj
• 82,840 points
4,517 views
0 votes
1 answer

How to provide a mysql database connection in single file in nodejs?

Hello @kartik, You could create a db wrapper ...READ MORE

answered Oct 15, 2020 in Node-js by Niroj
• 82,840 points
9,080 views
0 votes
1 answer

Truffle tests not running after truffle init

This was a bug. They've fixed it. ...READ MORE

answered Sep 11, 2018 in Blockchain by Christine
• 15,790 points
1,922 views
0 votes
1 answer

Hyperledger Sawtooth vs Quorum in concurrency and speed Ask

Summary: Both should provide similar reliability of ...READ MORE

answered Sep 26, 2018 in IoT (Internet of Things) by Upasana
• 8,620 points
1,451 views
0 votes
1 answer

Start script missing error when running npm start

It seems that there is an undefined ...READ MORE

answered Feb 10, 2022 in Java by Soham
• 9,710 points
4,438 views
0 votes
1 answer

How to schedule a google meet and get the meet link in NodeJs?

To create a Google Meet, you'll need ...READ MORE

answered May 27, 2022 in Node-js by Neha
• 9,020 points
3,874 views
0 votes
1 answer

How to use pino-transport in nodejs for logs?

Ensure that you have a recent version ...READ MORE

answered Jun 9, 2022 in Node-js by Neha
• 9,020 points
5,220 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP