AngularFire 2 - Auth.logout()回调 [英] AngularFire 2 - Auth.logout() callback

查看:145
本文介绍了AngularFire 2 - Auth.logout()回调的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试注销,然后导航到登录网址,但是来自此网址的authguard会阻止登录的用户查看它,并且自从解决承诺之前到达第二行,您需要单击方法事件两次使其工作。

  logout(){
this.angularfire.auth.logout();
this.router.navigate(['']);





$ b

有没有一种方法来实现router.navigate内的回调时,承诺解决?我尝试过然后(),但无论我的语法是不正确的,或有一个问题,认证类型...

解决方案

AngularFire2的 logout 应该的行为方式类似于底层的Firebase SDK。幸运的是,几天前,合并了一个PR ,它改变了注销返回一个承诺 - 所以下一个版本将解决您的问题。



在此之前,你可以听 auth 更改以确定何时可以导航:

  import rxjs /添加/操作者/过滤器; 
导入rxjs / add / operator / first;



logout(){
this.angularfire.auth
//您只想要未经认证的状态:
.filter ((authState)=>!authState)
//你只想要第一个未经认证的状态:
.first()
//你现在应该可以浏览:
.subscribe(()=> this.router.navigate(['']));
//组成的observable完成,所以不需要退订。
this.angularfire.auth.logout();
}


I'm trying to logout and then navigate to a login url, but the authguard from this url prevents logged users to view it and since the second line is reached before the promise is resolved you need to click the method event twice to make it work.

logout(){
    this.angularfire.auth.logout();
    this.router.navigate(['']);
  }

Is there a way to implement router.navigate inside a callback when the promise is resolved? I've tried then(), but either my syntax was incorrect or there is a problem with auth typings...

解决方案

AngularFire2's logout should really behave in a manner similar to the underlying Firebase SDK. Fortunately, a few days ago, a PR was merged that changes logout to return a promise - so the next release will address your issue.

Until then, you could listen to the auth changes to determine when it's okay to navigate:

import "rxjs/add/operator/filter";
import "rxjs/add/operator/first";

...

logout(){
  this.angularfire.auth
    // You only want unathenticated states:
    .filter((authState) => !authState)
    // You only want the first unathenticated state:
    .first()
    // You should now be able to navigate:
    .subscribe(() => this.router.navigate(['']));
    // The composed observable completes, so there's no need to unsubscribe.
  this.angularfire.auth.logout();
}

这篇关于AngularFire 2 - Auth.logout()回调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆