AngularFire 2 - Auth.logout() callback

前端 未结 2 1797
慢半拍i
慢半拍i 2021-01-19 15:01

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 prom

相关标签:
2条回答
  • 2021-01-19 15:14

    This has now been replaced with signOut() which returns a firebase promise. So if you import import { AngularFireAuth } from 'angularfire2/auth';

    you can then

    constructor(public afA: AngularFireAuth){}
    
    logout(){
        this.afA.auth.signOut().then(() => {
           this.router.navigate(['']);
        });
    }
    
    0 讨论(0)
  • 2021-01-19 15:23

    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();
    }
    
    0 讨论(0)
提交回复
热议问题