Firebase将存储在本地存储中的用户数据检索为firebase:authUser: [英] Firebase retrieve the user data stored in local storage as firebase:authUser:
问题描述
我正在使用Firebase和AngularJS。我正在使用Auth身份验证进行谷歌登录,我完成了这个过程。现在我需要检索存储在本地存储中的用户数据,如firebase:authUser:。
I am working with Firebase and AngularJS. I am using Auth authentication for google login and i completed the process.Now i need to retrieve the user data that is stored in local storage like firebase:authUser:.
一次我使用本地存储中的谷歌帐户登录你有firebase:authUser:.created我需要获取这些详细信息。
Once i login with google account in local storage you have firebase:authUser:.created and i need to fetch these details.
我使用以下方法存储用户数据
I used the following method to store user data
firebase.database().ref('users/' + user.uid).set
({
name: user.displayName,
email: user.email,
token: token
});
推荐答案
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 :
如果没有用户登录且未指定持久性,则将应用默认设置(<$ c浏览器应用中的$ c>本地。
所以 localStorage 在 Firebase JS SDK开源之前,code>是默认值,用于确认我的原始答案。
So localStorage
is the default which confirms my original answer before the Firebase JS SDK was open sourced.
这篇关于Firebase将存储在本地存储中的用户数据检索为firebase:authUser:的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!