【问题标题】:What is the proper way to sign out of an Angular application using Firebase Authentication and Firestore?使用 Firebase 身份验证和 Firestore 退出 Angular 应用程序的正确方法是什么?
【发布时间】:2020-09-27 10:37:36
【问题描述】:

使用 Firebase 身份验证和 Firestore 退出 Angular 应用程序的正确方法是什么?

我可以使用 Google 身份验证登录和退出,但有一些困难。我的目标是当用户点击注销按钮时:

  • 他们已退出网站及其 Google 帐户。
  • 他们所有的 Firestore 数据都被清除了。

相反,我发现在移动 Chrome 上(出于某种原因),他们的 Google 登录没有被清除,这意味着在不清除浏览器数据的情况下切换用户帐户是不可能的。 (编辑:我已经能够通过从 auth 对象中删除用户来获得这种行为;代码如下。)

我也经常收到以下错误——相当频繁,但我无法预测它何时会发生。 (它发生在开发和部署应用程序时。)我怀疑这与注销问题有关,因为我可以通过删除 IndexedDB 对象来修复错误。

我的退出代码来自this answerthis video。我在我的数据库适配器服务(下面的dbService)中调用了一个方法。 (路由器导航是一种清除浏览器的尝试。)

this.dbService.signOut().then( (value) => {
  this.router.navigate(['/' ])
    .then( (value) => { 
      window.location.reload();
     });
} );

this.dbService.signOut() 如下所示。 (dbService 是一个服务。)

  signOut() : Promise<void> {
    // unsubscribe to ensure no memory leaks
    this.productAdapter.destroy();
    this.customersAdapter.destroy();
    this.customerTypesAdapter.destroy();
    this.employeesAdapter.destroy();
    this.transactionsAdapter.destroy();
    this.preordersAdapter.destroy();
    this.giftsAdapter.destroy();
    this.nonconsumablesAdapter.destroy();

    return this.afAuth.signOut()
    .then( () => {
      /// this successfully signs the user out of his/her google account
      this.afAuth.currentUser.then( (user : firebase.User | null) => {
          user?.delete();
      } );
      this.clearLocalData();
    });
   }

我也在不同的时间尝试过以下几行,以尝试清除数据。

localStorage.clear();
await window.indexedDB.deleteDatabase("firebaseLocalStorageDb");
await window.indexedDB.deleteDatabase("firestore/[DEFAULT]/XXXXXXX/main");
/// as well, deleting all the cookies

下面有我的依赖项。唯一没有直接从 Firebase 文档中取出的是 firebase-angular

  "dependencies": {
    "@angular/animations": "^9.1.9",
    "@angular/cdk": "^9.2.4",
    "@angular/common": "^9.1.9",
    "@angular/compiler": "^9.1.9",
    "@angular/core": "^9.1.9",
    "@angular/fire": "^6.0.0",
    "@angular/forms": "^9.1.9",
    "@angular/localize": "^9.1.9",
    "@angular/material": "^9.2.4",
    "@angular/material-moment-adapter": "^9.2.4",
    "@angular/platform-browser": "^9.1.9",
    "@angular/platform-browser-dynamic": "^9.1.9",
    "@angular/router": "^9.1.9",
    "@angular/service-worker": "^9.1.9",
    "@google-cloud/firestore": "^3.8.4",
    "firebase": "^7.14.6",
    "firebase-admin": "^8.12.1",
    "firebase-functions": "^3.6.2",
    "firebaseui": "^4.5.0",
    "firebaseui-angular": "^5.0.0",
    "hammerjs": "^2.0.8",
    "moment": "^2.26.0",
    "rxjs": "~6.5.5",
    "tslib": "^1.13.0",
    "zone.js": "~0.10.2"
  },

编辑: 如果我不启用磁盘持久性,错误就会消失。但我的应用程序需要磁盘持久性。

我尝试将 FirebaseUI 配置更改为 signInFlow: 'redirect',。无论如何,这可能是一个更好的选择(因为这适用于移动应用程序),但它并没有解决这个问题。

【问题讨论】:

  • 你能贴出实际有this.afAuth.signOut()行的代码

标签: angular firebase google-cloud-firestore firebase-authentication angularfire


【解决方案1】:

最后我已经从我的项目中删除了firebaseui-angular,我只是使用 Firebase 的 JS API 进行身份验证。我很可能错误地使用了firebaseui-angular,但我无法弄清楚。使用普通的 Firebase JS API 似乎解决了我的问题。

【讨论】:

    猜你喜欢
    • 2019-04-23
    • 2021-07-04
    • 2012-11-18
    • 2021-05-29
    • 2021-08-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-23
    相关资源
    最近更新 更多