Firebase:为什么`onAuthStateChanged`总是返回用户?

时间:2019-12-10 02:28:56

标签: javascript firebase firebase-authentication

我删除了我所有的cookie,在localstorage或IndexDB中都没有。

但是,onAuthStateChanged始终会产生一个用户。

https://firebase.google.com/docs/reference/js/firebase.auth.Auth

文档介绍了如何添加可观察对象。但这并没有解释它会导致什么副作用。

是否在cookie中添加了会话ID?

这如何连续产生一个没有任何持久客户端的用户?

firebaseClient.auth().createUserWithEmailAndPassword(email, password).catch(function (error) {

   ... the user is ALWAYS here.

我希望Firebase文档能更详细地说明幕后发生的事情。
太令人沮丧了!

1 个答案:

答案 0 :(得分:2)

Firebase Auth根据平台,可用性和身份验证状态持久性设置在内部使用内存,LocalStorage,SessionStorage和IndexDB。从存储中删除数据将导致随后的onAuthStateChanged()返回null

您是否在删除Cookie,本地存储/ indexDB之后刷新应用程序?确定要删除indexDB中的所有内容吗?

以下是Firebase身份验证用户初始化的代码段:

fireauth.storage.UserManager.prototype.initialize_ = function() {
  var self = this;
  // Local key.
  var localKey = fireauth.storage.UserManager.getAuthUserKey_(
      fireauth.authStorage.Persistence.LOCAL);
  // Session key.
  var sessionKey = fireauth.storage.UserManager.getAuthUserKey_(
      fireauth.authStorage.Persistence.SESSION);
  // In memory key. This is unlikely to contain anything on load.
  var inMemoryKey = fireauth.storage.UserManager.getAuthUserKey_(
      fireauth.authStorage.Persistence.NONE);
  // Migrate any old currentUser from localStorage to indexedDB.
  // This keeps any user signed in without the need for reauthentication and
  // minimizes risks of dangling Auth states.
  return this.manager_.migrateFromLocalStorage(
      localKey, this.appId_).then(function() {
    // Check if state is stored in session storage.
    return self.manager_.get(sessionKey, self.appId_);
  }).then(function(response) {
    if (response) {
      // Session storage is being used.
      return sessionKey;
    } else {
      // Session storage is empty. Check in memory storage.
      return self.manager_.get(inMemoryKey, self.appId_)
          .then(function(response) {
            if (response) {
              // In memory storage being used.
              return inMemoryKey;
            } else {
              // Check local storage.
              return self.manager_.get(localKey, self.appId_)
                  .then(function(response) {
                    if (response) {
                      // Local storage being used.
                      return localKey;
                    } else {
                      // Nothing found in any supported storage.
                      // Check current user persistence in storage.
                      return self.manager_.get(
                          fireauth.storage.UserManager.PERSISTENCE_KEY_,
                          self.appId_).then(function(persistence) {
                            if (persistence) {
                              // Sign in with redirect operation, apply this
                              // persistence to any current user.
                              return fireauth.storage.UserManager
                                  .getAuthUserKey_(persistence);
                            } else {
                              // No persistence found, use the default.
                              return localKey;
                            }
                          });
                    }
                  });
            }
          });
    }
  }).then(function(currentKey) {
    // Set current key according to the persistence detected.
    self.currentAuthUserKey_ = currentKey;
    // Make sure only one state available. Clean up everything else.
    return self.removeAllExcept_(currentKey.persistent);
  }).thenCatch(function(error) {
    // If an error occurs in the process and no current key detected, set to
    // persistence value to default.
    if (!self.currentAuthUserKey_) {
      self.currentAuthUserKey_ = localKey;
    }
  });
};