Firebase 检索存储在本地存储中的用户数据作为 firebase:authUser: [英] Firebase retrieve the user data stored in local storage as firebase:authUser:

查看:34
本文介绍了Firebase 检索存储在本地存储中的用户数据作为 firebase:authUser:的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Firebase 和 AngularJS.我正在使用 Auth 身份验证进行 google 登录,并完成了该过程.现在我需要检索存储在本地存储中的用户数据,如 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:.

一旦我在本地存储中使用 google 帐户登录,您就拥有了 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 会设置 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.

这篇关于Firebase 检索存储在本地存储中的用户数据作为 firebase:authUser:的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆