【问题标题】:Request not being sent during logout procedure注销过程中未发送请求
【发布时间】: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


    【解决方案1】:

    你需要 subscribe 到 observables 才能“解雇”它们。

    this.authHttp.post(this.endpoint_url + '/auth/logout','').subscribe((res)=>{
        if(res.status == 204){  
         alert('logout successful');
         this.currentUser = null;
         localStorage.removeItem('token');
       }else {alert('logout failed');}           
    }); 
    

    如果您需要在注销成功后移除令牌,则需要在函数回调 (subscribe) 中执行此操作。

    【讨论】:

    • 是的,它帮助(部分),以及其他答案(取消点击事件)。现在它执行了,但是在令牌被删除(因此失败)之后。
    • 哦,您想在之后删除令牌吗?检查我更新的答案。您可以删除 res.status == 204 内的令牌或订阅内您喜欢的任何地方
    • 原来另一个问题出在服务器端,再加上 jwt 配置错误(由于其他组件中的另一个错误,它似乎正在工作)。最初的问题是没有使用subscribe。另外,我应该取消活动。
    • @wondra 啊,很高兴你知道了。
    猜你喜欢
    • 1970-01-01
    • 2020-04-23
    • 1970-01-01
    • 2018-01-24
    • 2018-08-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多