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

https://codesandbox.io/s/lively-tree-hd0fo

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

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

Jun 16 in Node-js by Vaani
• 7,020 points
18 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 by Neha
• 8,560 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,720 points
8,040 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,720 points
1,169 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,720 points
2,224 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,720 points
4,036 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
970 views
0 votes
1 answer
0 votes
1 answer

Start script missing error when running npm start

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

answered Feb 10 in Java by Soham
• 8,730 points
1,812 views
0 votes
1 answer
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 in Node-js by Neha
• 8,560 points
44 views
webinar REGISTER FOR FREE WEBINAR X
Send OTP
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP