0

On frontend I am checking if my JWT is expired.

Function is:

tokenExpired(token: string) {
    const expiry = JSON.parse(window.atob(token.split('.')[1])).exp;
    return Math.floor(new Date().getTime() / 1000) >= expiry;
  }

And then using this function like:

 if (!this.tokenExpired(this.jwtToken || '')) {
      this.getAccountData().subscribe(
        (data) => {
          this.accout = data;
          this.isLoggedIn();
        },
        (err) => {
          this.isLoggedIn$.next(2);
        }
      );
    } else {
      this.isLoggedIn$.next(2);
    }

When I build my application it gives me an error in console.log: Failed to execute 'atob' on 'Window': The string to be decoded is not correctly encoded.

What are some alternatives to use instead of atob or window.atob?

Thank you!

SOLVED --> solution:

 tokenExpired(token: string) {
    if (token !== '') {
      const expiry = JSON.parse(atob(token.split('.')[1])).exp;
      return Math.floor(new Date().getTime() / 1000) >= expiry;
    } else {
      return false;
    }
  }

Is this solution a good practice? It doesn't give me an error

devZ
  • 606
  • 1
  • 7
  • 23
  • ~~Are you in the GUI thread of a browser? Try without the `window.` part.~~ Also, `Date.now()` (instead of `new Date().getTime()`) – tevemadar Jul 05 '22 at 09:18
  • I tried with and without window. and both times same error on when using built application. (on ng serve it works, on ng build it doesn't) – devZ Jul 05 '22 at 09:20
  • Does this answer your question? [Failed to execute 'atob' on 'Window'](https://stackoverflow.com/questions/22578530/failed-to-execute-atob-on-window) – tevemadar Jul 05 '22 at 09:21
  • 1
    Yep, I missed reading the error message. Duplicate suggests `atob()` produces this message when the passed data is not Base64. Consider checking if that token is not an error message or some other unexpected content. – tevemadar Jul 05 '22 at 09:23
  • Check my edited question. I think I solved problem but I would be very happy if someone could give me feedback if this solution is OK – devZ Jul 05 '22 at 09:32
  • `if(!this.tokenExpired(this.jwtToken || ''))` is the part where you can end up with passing an empty string, and then you have to deal with it inside `tokenExpired()`. I consider this suspicious, especially the latter part. If it is acceptable that the token does not exist, and the code should pass in that case, I would rather write the `if` as `if(!this.jwtToken || !this.tokenExpired(this.jwtToken))` and revert `tokenExpired()` to its original form. – tevemadar Jul 05 '22 at 10:09

1 Answers1

-1

Instead of

const expiry = JSON.parse(window.atob(token.split('.')[1])).exp;

Use

const expiry = JSON.parse(atob(token.split('.')[1])).exp;
Shriyank
  • 47
  • 6
  • Failed to execute 'atob' on 'Window': The string to be decoded is not correctly encoded. – devZ Jul 05 '22 at 09:29
  • It looks like you are suggesting accessing `window` implicitly instead of explicitly. I don't see how that could help. If the variable was shadowed it might cause more problems. – Quentin Jul 05 '22 at 09:54