Search code examples
javascriptnode.jsjwtexpress-jwt

how to verify jwt token in nodejs / never expire?


I implemented JWT token in node js.I am able to generate jwt token.Now I want to check when it will expire or invalid . I check the documentation it says after 120ms it will expire .but I my token is not expire .it always decode the token why ?

I generate the token like this '

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..etc


Solution

  • You can use expiresIn option to do this. Example based on your code:

    const token = jwt.sign(payload, "jhjhhj", {
      algorithm: "HS256",
      issuer: "jjj",
      subject: `${user.userId}`,
      expiresIn: "10m"
    });