更新时间:2023-01-06 21:37:25
Web SDK 的文档没有提到在成功验证后,Firebase 会设置 User 在 localStorage 中的对象.API 参考也没有提到这一点.我在尝试使用 Firebase 配置我的 Vue 应用程序时通过检查 localStorage 发现了这一点.
The documentation for the Web SDK does not mention that upon successfully authentication, Firebase sets the User object in localStorage. The API reference does not mention this either. I discovered this the same way you have by examining localStorage while trying to configure my Vue app with Firebase.
要从 localStorage 检索用户,您可以执行以下操作:
To retrieve the user from localStorage, you can do the following:
const authUser = Object.keys(window.localStorage)
.filter(item => item.startsWith('firebase:authUser'))[0]
您可以在 localStorage
中有多个条目,这就是 filter()
You could have multiple entries in localStorage
so that's the reason for filter()
见下面 Metallica 的回答.
See Metallica's answer below.
更新 (2018-02-21):现在似乎有一个部分 (Web)身份验证状态持久性.不确定当我最初发布我的答案时是否存在这个问题,但我正在更新此答案以链接它,因为这个问题得到了适度的关注.
Update (2018-02-21): It seems there is now a section (Web) Authentication State Persistence . Unsure if this was there when I originally posted my answer, but I'm updating this answer to link it since this questions gets moderate attention.
更新 2 (2018-02-21):如持久化行为概述:
Update 2 (2018-02-21): As stated under Overview of persistence behavior :
如果没有用户登录且未指定持久性,则将应用默认设置(浏览器应用中的local
).
If no user is signed in and no persistence is specified, the default setting will be applied (
local
in a browser app).
所以 localStorage
是默认值,它在 Firebase JS SDK 开源之前确认了我的原始答案.
So localStorage
is the default which confirms my original answer before the Firebase JS SDK was open sourced.