【问题标题】:Firebase not logging user outFirebase 未注销用户
【发布时间】:2019-01-27 20:12:35
【问题描述】:

我使用 Firebase 的注销功能来注销登录的用户。我可以正确登录,但我显然无法注销。我已经对此进行了多次测试,但注销功能根本不起作用。任何和所有的帮助将不胜感激。以下是我的代码:

export class AuthService {
    private user: Observable<firebase.User>;
    private userDetails: firebase.User = null;

    constructor(private _firebaseAuth: AngularFireAuth, private router: Router) { 
        this.user = _firebaseAuth.authState;
        this.user.subscribe(
            (user) => {
                if (user) {
                    this.userDetails = user;
                    console.log(this.userDetails);
                }
                else {
                    this.userDetails = null;
                }
            }
        );
    }

  signInWithGoogle() {
      return this._firebaseAuth.auth.signInWithRedirect(
          new firebase.auth.GoogleAuthProvider()
      )
  }

  signup(email: string, password: string) {
      this._firebaseAuth.auth.createUserWithEmailAndPassword(email, password)
          .then(value => {
              console.log('Success!', value);
          })
          .catch(err => {
              console.log('Something went wrong:',err.message);
          });    
  }

  login(email: string, password: string) {
      this._firebaseAuth.auth.signInWithEmailAndPassword(email, password)
          .then(value => {
              console.log('Nice, it worked!');
          })
          .catch(err => {
              console.log('Something went wrong:',err.message);
          });
  }

  isLoggedIn() {
      if (this.userDetails == null ) {
          return false;
      } else {
          return true;
      }
  }
  directToNext() {
      if (this.isLoggedIn){
          this.router.navigate(['/or-items/1']);
      }    
  }

  logout() {
      this._firebaseAuth.auth.signOut()
      .then((res) => this.router.navigate(['/']));
    }
  }

然后在 HTML 中:

<script>
    import {AuthService} from' ./../AuthService';
    
    function  logout() {
        this.authService.logout();
    }
    
    function  isLoggedIn() {
        this.authService.isLoggedIn();
    }
 </script>
 
<span>
 <button mat-raised-button color="basic" (click)="logout()">
          logout
 </button>
</span>

我知道用户未正确注销,因为 firebase 控制台显示该用户仍处于登录状态。

【问题讨论】:

  • 不应该是AuthService.logout();AuthService.isLoggedIn()吗?
  • 如果没有,您可能需要将AuthService 添加到页面的构造函数/init,因为this.authService 没有指向我可以看到的任何内容。

标签: html angular typescript firebase firebase-authentication


【解决方案1】:

Qari,AFAIK,Firebase 控制台不指示用户是否已登录。我认为它显示的是上次登录日期。

至少在 Android Firebase SDK 上,当请求注销时,有一个回调可用于指示调用是否成功。可以肯定的是,可以再次获取当前用户信息并验证没有当前用户。您可能想尝试类似的方法。

【讨论】:

  • 感谢您提供的信息。这很有帮助。您知道我如何检查以确认用户已注销吗?我遇到了我想使用 *ngIf= isLoggedIn() 然后显示按钮的问题。但是,考虑到登录/退出的状态,按钮永远不会消失/出现
  • 我只将 Firebase 与 Java 代码 (Android) 一起使用,解决方案就是我上面描述的。
猜你喜欢
  • 2021-11-23
  • 1970-01-01
  • 2018-07-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-07-09
  • 1970-01-01
相关资源
最近更新 更多