How can you verify if a JWT is still valid?

You can have your client side decode the JWT and check an expiry field and compare it with system time.

eg.

  isExpired: (token) => {
    if (token && jwt.decode(token)) {
      const expiry = jwt.decode(token).exp;
      const now = new Date();
      return now.getTime() > expiry * 1000;
    }
    return false;

you can use npm install jsonwebtoken or some other npm package on the client side to do this


An elegant solution to handle token expiration is when you set the token(in LocalStorage or store(redux), or both) is also to have an Async function that runs exactly when the token expires. Something like this:

const logUserOut = token =>{
    setTimeout(()=> MyLogoutFunction(), token.expiresIn)
}

This way you make sure that the user won't be logged when the token is no longer valid.