简体   繁体   English

如果我们有效的令牌,Angular6 auth0 / angular2-jwt isTokenExpired函数始终返回false

[英]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. 我正在使用auth0 / angular2-jwt pakage处理Angular6应用程序中基于JWT令牌的身份验证。 if we gave valid token isTokenExpired function always returns false 如果我们给了有效的令牌isTokenExpired函数总是返回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 我尝试了以上两种方法,它总是返回false

Thanks in Advance. 提前致谢。

You should use { tokenNotExpired } 'angular2-jwt'; 您应该使用{tokenNotExpired}'angular2-jwt'; You will need following implementation: 您将需要以下实现:

Inside your app.module.ts 在您的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') 使用tokenNotExpired('id_token')

The link below might help. 以下链接可能会有所帮助。 Good luck. 祝好运。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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