簡體   English   中英

Firebase:為什么`onAuthStateChanged` 總是返回一個用戶?

[英]Firebase: Why does `onAuthStateChanged` always return a user?

我已經刪除了我所有的 cookie,本地存儲或 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 文檔能更具體地說明幕后發生的事情。
太令人沮喪了!

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;
    }
  });
};

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM