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

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

问题描述

我正在尝试注销然后导航到登录 url,但是来自该 url 的 authguard 阻止登录的用户查看它,并且由于在 promise 解决之前到达了第二行,因此您需要单击方法事件两次才能让它起作用.

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(['']);
  }

有没有办法在承诺解决后在回调中实现 router.navigate ?我试过 then(),但要么是我的语法不正确,要么是身份验证类型有问题...

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 的 logout 应该真正以类似于底层 Firebase SDK 的方式运行.幸运的是,前几天合并了一个PR,改变了logout 返回承诺 - 这样下一个版本将解决您的问题.

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.

在那之前,您可以监听 auth 更改以确定何时可以导航:

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天全站免登陆