question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

handle "TokenExpiredError: jwt expired" Error

See original GitHub issue
const checkToken = (req, res, next)=>{
    let token = req.headers['x-access-token'] || req.headers['authorization'];
    if(token && token.startsWith('Bearer ')){
        token = token.slice(7, token.length)
    }
    if(token){
        try{
            let decoded = jwt.verify(token, secret);
            req.decoded = decoded;
            User.findOne({email:decoded.email})
                .populate('following')
                .exec((err, user)=>{
                    if(err) throw err;
                    if(user){
                        req.user = user;
                        next();
                    }
                });
        }catch(err){
            res.status(403).send({
                success:false,
                message:err
            })
        }
    }else{
        return res.status(403).json({
            success:false,
            message:'Unauthorized'
        })
    }
};

If Token is Expired. Getting this error:

{ TokenExpiredError: jwt expired at /Users/pradeepkumar/Desktop/bitbucket/volupn-api/node_modules/jsonwebtoken/verify.js:152:21 at getSecret (/Users/pradeepkumar/Desktop/bitbucket/volupn-api/node_modules/jsonwebtoken/verify.js:90:14) at Object.module.exports [as verify] (/Users/pradeepkumar/Desktop/bitbucket/volupn-api/node_modules/jsonwebtoken/verify.js:94:10) at Socket.socket.on (/Users/pradeepkumar/Desktop/bitbucket/volupn-api/sockets/base.js:10:17) at Socket.emit (events.js:182:13) at Socket.EventEmitter.emit (domain.js:441:20) at /Users/pradeepkumar/Desktop/bitbucket/volupn-api/node_modules/socket.io/lib/socket.js:528:12 at process._tickCallback (internal/process/next_tick.js:61:11) name: 'TokenExpiredError', message: 'jwt expired', expiredAt: 2019-04-14T13:12:19.000Z }

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:9 (2 by maintainers)

github_iconTop GitHub Comments

10reactions
TeijiWcommented, Aug 20, 2019

I’m using err.name, but he just returns “Error

7reactions
Shhuycommented, Dec 31, 2019

是否是到了设置的过期时间以后进行解密,就会进入异常处理,我需要再时间过期以后拿到存入的信息,但是现在进入了异常。怎么解决呢?

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to deal with a TokenExpiredError: jwt expired?
Create /check-token endpoint that will check if the current token is still valid. Create token when user logs in. Token will be valid...
Read more >
How to use the jsonwebtoken.TokenExpiredError function in ...
warn('Access token expired'); done(verifyError); } else if (verifyError instanceof jwt.JsonWebTokenError) { log.warn('An error was received validating the token ...
Read more >
How do you resolve 'jwt expired' error
Hello,. I customised the Ping Create Users API for a local application. When I try to test it in Postman I keep hitting...
Read more >
Knowledgebase - Token Expired Error - AuthRocket
Q: I'm getting an error TokenExpiredError: jwt expired . Is this a problem on your end or mine? A: Most likely, the token...
Read more >
How to use name function in JsonWebTokenError - Tabnine
jwt.verify(token, Secret, verifyOptions, function (err, decoded) { if (err) { if (err.name === 'TokenExpiredError') return res.json({ status: "Failure", ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found