AngularFire 2-Auth.logout()回调

cer*_*lex 3 firebase firebase-authentication angularfire2 angular

我正在尝试注销,然后导航到登录URL,但是此URL中的authguard阻止了登录的用户查看它,并且由于在解决诺言之前已到达第二行,因此您需要单击两次方法事件以使其起作用。

logout(){
    this.angularfire.auth.logout();
    this.router.navigate(['']);
  }
Run Code Online (Sandbox Code Playgroud)

解决承诺后,是否可以在回调中实现router.navigate?我已经尝试了then(),但是我的语法不正确,或者验证类型有问题...

小智 8

现在已替换为signOut()which 返回 firebase 承诺。所以如果你导入import { AngularFireAuth } from 'angularfire2/auth';

然后你可以

constructor(public afA: AngularFireAuth){}

logout(){
    this.afA.auth.signOut().then(() => {
       this.router.navigate(['']);
    });
}
Run Code Online (Sandbox Code Playgroud)


car*_*ant 5

AngularFire2的logout行为应与基础Firebase SDK相似。幸运的是,几天前,PR被合并以更改logout以返回承诺-因此下一个版本将解决您的问题。

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

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();
}
Run Code Online (Sandbox Code Playgroud)