简体   繁体   中英

Angular6 auth0/angular2-jwt isTokenExpired function always return false if we valid Token

I'm using auth0/angular2-jwt pakage for handling JWT Token Based authentication in Angular6 Application. if we gave valid token isTokenExpired function always returns false

The below code i have tried:

   localStorage.setItem('id_token', response['token']);
   const helper = new JwtHelperService();
   helper.isTokenExpired(localStorage.getItem('id_token')

And

constructor(
        private http: Http,
        private helper: JwtHelperService
    ) {
    }

public handleResponse(response: Response){
    localStorage.setItem('id_token', response['token']);  
 console.log(this.helper.isTokenExpired(localStorage.getItem('id_token')));

}

i have tried above two methods its always return false

Thanks in Advance.

You should use { tokenNotExpired } 'angular2-jwt'; You will need following implementation:

Inside your app.module.ts

import { tokenNotExpired } from 'angular2-jwt';
. 
.
.
 @NgModule({

      providers: [
       .
       .
       { provide: 'tokenNotExpired', useValue: tokenNotExpired }, 
      ]

 })

and Inside your service where you need to check if toker is expired or not:

constructor( @Inject('tokenNotExpired') private tokenNotExpired)

isTokenExpired(idToken){
    return this.tokenNotExpired(idToken);
}

Use tokenNotExpired('id_token')

The link below might help. Good luck.

https://github.com/auth0/angular2-jwt/issues/334#issuecomment-293968046

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM