Question

Jwt suddenly stops working on protected route

I am running Nodejs API application on the droplet. I was able to login to my app and access the protected route. But on accessing the route the following day, I started getting TokenExpiredError: jwt expired on accessing any protected route. It will work well again after I ran Pm2 kill and re-deploy again


Submit an answer

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Want to learn more? Join the DigitalOcean Community!

Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in Q&A, subscribe to topics of interest, and get courses and tools that will help you grow as a developer and scale your project or business.

I have resolved the issue. I was setting the expiry date at the global context as a constant variable. Thanks

Any updates on this?


const handleJWT = async (req, res, next) => async (err, user, info) => {
  const error = err || info;
  const logIn = Promise.promisify(req.logIn);
  const apiError = new APIError({
    message: error
      ? error.message
      : "Authentication required: Authentication with a valid API Key is required.",
    status: httpStatus.UNAUTHORIZED,
    stack: error ? error.stack : undefined,
    errors: "",
    isPublic: true,
    title: ""
  });

  try {
    // if user is not login
    if (error || !user) throw error;
    await logIn(user, { session: false });
  } catch (e) {
    return next(apiError);
  }

  req.user = user;
  return next();
};

export const authorize = () => async (req, res, next) =>
  passport.authenticate(
    "jwt",
    { session: false, failWithError: true },
    await handleJWT(req, res, next)
  )(req, res, next);

From the error trace, it points to

passport.authenticate(
    "jwt",
    { session: false, failWithError: true },
    await handleJWT(req, res, next)
  )(req, res, next);