【发布时间】:2017-07-29 08:44:58
【问题描述】:
是否可以在 Angular 2 应用程序的组件中检查 id 令牌是否已过期?我得到了一个 AuthService 方法
public isAuthenticated(): boolean {
/* check if id_token is expired or not */
return tokenNotExpired();
}
在模板中使用它工作正常。如果用户退出,则返回 false,在用户登录后,角度变化检测重新运行模板中的函数并返回 true。
在组件内部使用
@Component({
selector: 'app',
providers: [
Auth
],
templateUrl: 'app.template.html'
})
export class AppComponent implements OnInit {
public isAuthorized: Object = {};
constructor(private auth: Auth) {
this.auth.handleAuthentication();
}
ngOnInit() {
console.log(this.auth.isAuthenticated());
}
}
用户登录后它不会更新。需要刷新页面。我该如何解决这个问题?
【问题讨论】: