【发布时间】:2017-05-23 23:04:34
【问题描述】:
我有简单的 Angular2 应用程序,如果用户点击
<a class="dropdown-item" (click)=authService.logout() href="/welcome">Log out</a>
注销菜单项,AuthenticationServices的logout()方法执行
logout() {
alert('loging out');
this.authHttp.post(this.endpoint_url + '/auth/logout','').map(res => {
if(res.status == 204) alert('logout successful');
else alert('logout failed');
});
this.currentUser = null;
localStorage.removeItem('token');
}
问题是部分
this.authHttp.post(this.endpoint_url + '/auth/logout','').map(res => {
if(res.status == 204) alert('logout successful');
else alert('logout failed');
});
似乎从未被执行过。使用 authHttp(angular2-jwt) 访问受保护的 API 可以在应用程序的其他部分正常工作,没有任何问题。
弹出第一个警报“注销”,用户被注销并重定向到“/welcome”,并且不能再访问 API 的私有部分。但是,在浏览器和服务器日志中都没有对“/auth/logout”的请求。
还有AuthGuard 服务可以重定向任何未登录的用户:
canActivate() {
//loggedIn() {!(this.currentUser == null);}
if (!this.auth.loggedIn()) {
this.router.navigate(['/welcome']);
return false;
}
return true;
}
【问题讨论】:
标签: javascript angular httprequest jwt rxjs