且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Firebase将存储在本地存储中的用户数据检索为firebase:authUser:

更新时间:2023-01-06 21:47:25

Web SDK的文档没有提及成功进行身份验证后,Firebase会在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检索用户,您可以执行以下操作: / p>

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()的原因

编辑:请参阅下面的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.

Update 2(2018-02 -21):如持久性行为概述$ b $所述b

Update 2 (2018-02-21): As stated under Overview of persistence behavior :


如果没有用户登录且未指定持久性,则将应用默认设置(本地。

所以 localStorage 在 Firebase JS SDK开源之前,code>是默认值,用于确认我的原始答案

So localStorage is the default which confirms my original answer before the Firebase JS SDK was open sourced.