Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JWT Token (Invalid token Specified)

Tags:

reactjs

jwt

I have module to check if the expiration date of token is already expired or not. so if ever the token is expired it will automatically the page will go to login again.

I already code it but their is error in my console and my page is loading anymore.

Uncaught InvalidTokenError {message: "Invalid token specified"}

Image

List not working

  • I read some post it says need to put this. { header: true }
  • Need specify if the localstorage has token

This is my code:

        if (localStorage.getItem("token") === null) {
        let token_expired = localStorage.getItem('token');
        let decoded = decode(token_expired, { header: true });
        let decode_string = decoded["exp"];
        var current_time = Date.now() / 1000;
        if(decode_string < current_time)
        {
            localStorage.clear();
        }
    }
like image 662
DevGe Avatar asked Nov 16 '25 04:11

DevGe


2 Answers

I think that your token does not exist.

  1. Go inside dev tools (ctrl +shift + i) then

  2. Select Application

  3. Inside the Application go to Storage then

  4. Open Local Storage (double click on local storage) then

  5. Clear local storage

like image 126
Tejsingh Avatar answered Nov 17 '25 18:11

Tejsingh


I think you need to change the first condition, and use declarative names for variables.

const storedToken = localStorage.getItem("token");
if (storedToken){
   let decodedData = decode(storedToken, { header: true });
   let expirationDate = decodedData.exp;
    var current_time = Date.now() / 1000;
    if(expirationDate < current_time)
    {
        localStorage.removeItem("token");
    }
 }
like image 38
Mohammed Essehemy Avatar answered Nov 17 '25 18:11

Mohammed Essehemy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!