gpt4 book ai didi

javascript - Firebase:为什么 `onAuthStateChanged` 总是返回一个用户?

转载 作者:行者123 更新时间:2023-11-30 13:46:18 27 4
gpt4 key购买 nike

我已经删除了我所有的 cookie,localstorage 或 IndexDB 中什么都没有。

然而,onAuthStateChanged 总是产生一个用户。

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

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

它是否在 cookie 中添加了一个 session-ids?

这如何在客户端上不保留任何内容的情况下持续生成用户?

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

关于javascript - Firebase:为什么 `onAuthStateChanged` 总是返回一个用户?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59259353/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com