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
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!
Hi @asmelitus, do you have any sample code? Maybe your JWT is actually getting expired. How do you generate a new JWT token and how often?
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);
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Full documentation for every DigitalOcean product.
The Wave has everything you need to know about building a business, from raising funding to marketing your product.
Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.
New accounts only. By submitting your email you agree to our Privacy Policy
Scale up as you grow — whether you're running one virtual machine or ten thousand.
Sign up and get $200 in credit for your first 60 days with DigitalOcean.*
*This promotional offer applies to new accounts only.