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.

tokenNotExpired() always return false

See original GitHub issue

Hi, I use your library without Ionic (native Angular 2) and all time tokenNotExpired() method return false.

When this code is exectued :

   loggedIn() {
    console.log(tokenNotExpired());
    return tokenNotExpired();
  }

  constructor(private router: Router) {}

  canActivate() {
    console.log(this.loggedIn());
    if(this.loggedIn()) {
      return true;
    } else {
      this.router.navigate(['login']);
      return false;
    }
  }

I use this code for angular routing canActivate: [AuthGuard].

But tokenNotExpired() always return false.

I don’t understand why !

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:31 (2 by maintainers)

github_iconTop GitHub Comments

120reactions
qmsqcommented, Apr 13, 2017

I think I found the solution, as I was fighting with a similar error: The error is induced by Commit a6984d1df9315cc52b22b9c57b8badc4b6d1f47 (update token name). With this commit, angular2-jwt assumes that the standard name of the token is token , not any longer access-token. The problem can be traced to the Quickstart of Auth0 for Angular 2 (https://auth0.com/docs/quickstart/spa/angular2/01-login), as it states (as of time of writing this):

// app/auth.service.ts

import { Injectable }      from '@angular/core';
import { tokenNotExpired } from 'angular2-jwt';

// Avoid name not found warnings
declare var Auth0Lock: any;

@Injectable()
export class Auth {
  // Configure Auth0
  lock = new Auth0Lock('MyClientID', 'my.domain.dom', {});

  constructor() {
    // Add callback for lock `authenticated` event
    this.lock.on("authenticated", (authResult) => {
      localStorage.setItem('id_token', authResult.idToken);
    });
  }

  public authenticated() {
    // Check if there's an unexpired JWT
    // This searches for an item in localStorage with key == 'id_token'
    return tokenNotExpired();
  }

}

So there are two possible fixes: -Change in the constructor from localStorage.setItem('id_token', authResult.idToken) to localStorage.setItem('token', authResult.idToken)

-Use tokenNotExpired('id_token') instead of tokenNotExpired()

The third, impossible and stupid solution would be changing the default token name in JWT again.

8reactions
escardincommented, Apr 13, 2017

so return !isTokenExpired()

Read more comments on GitHub >

github_iconTop Results From Across the Web

angular2-jwt token alwas not valid - Stack Overflow
I have a service that verifies everytime a route is called if the jwt token is valid or not using the angular2-jwt tokenNotExpired()...
Read more >
Issues with the Angular2 Quickstart - Auth0 Community
Another of the issues is that the use of angular2-jwt is incorrect and does not work. There's an issue where tokenNotExpired always returns...
Read more >
How do you handle token refreshing? - Ionic Forum
So in the token provider I can just use something like angular2-jwt's tokenNotExpired() function that returns true or false depending on if ...
Read more >
Refreshing Tokens — flask-jwt-extended 4.4.4 documentation
The basic idea here is that at the end of every request, we will check if there is a JWT that is close...
Read more >
Authentication And Authorization In Angular - Part Twelve
Don't be scared by watching the above code, always write the simple code. ... So here we can simply return tokenNotExpired().
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