I solved this problem by adding a variable to the token data:
softexp - I set this to 5 mins (300 seconds)
I set the expiresIn option at the desired time before the user is forced to log in again. The mine is set for 30 minutes. This should be greater than softexp .
When a client-side application sends a request to the server API (where a token is required, such as a client list page), the server checks whether the supplied token remains or not, based on its original expiration value ( expiresIn ). If this is not valid, the server will respond the status defined for this error, for example. INVALID_TOKEN .
If the token is still valid based on the expiredIn value, but it has already exceeded the softexp value, the server will respond with a separate status to this error, for example. EXPIRED_TOKEN :
(Math.floor(Date.now() / 1000) > decoded.softexp)
On the client side, if he received an EXPIRED_TOKEN response, he should update the token automatically by sending an update request to the server. This is transparent to the user and automatically takes care of the client application.
The update method on the server should check if the token is valid:
jwt.verify(token, secret, (err, decoded) => {})
The server will refuse to update tokens if it has not completed the above method.
James A Aug 04 '17 at 21:29 2017-08-04 21:29
source share