Firebase(Web)-注册后添加用户数据 [英] Firebase (web) - add user data upon signup

查看:60
本文介绍了Firebase(Web)-注册后添加用户数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到以下情况的麻烦:

I am having some troubles with the following case:

我正在Firebase Auth中使用电子邮件登录功能.一切正常.但是,作为注册表格的一部分,有一个用户名字段.提交表单后,将执行以下操作:

I am using email login functionality in Firebase Auth. Everything works fine. As part of the registration form however, there is a user name field. When the form gets submitted, the following action is performed:

const response = await auth.createUserWithEmailAndPassword(email, password);
await response.user?.updateProfile({
   displayName: name
});

这可以使用显示名称正确地更新用户配置文件.问题在于,在 createUserWithEmailAndPassword 完成后,它立即触发 onAuthStateChanged ,这是在应用程序的本地状态下控制用户的唯一位置.但是,一旦触发了回调,显示名称仍然是未知的,这将导致注册后应用程序的第一个渲染不知道用户名.如果刷新页面,则会显示该名称,因为它一直存在.

This is properly updating the user profile with the display name. The problem is that right after createUserWithEmailAndPassword finishes, it fires onAuthStateChanged, which is the only place the user is being controlled in the app's local state. Once the callback is fired however, the display name is still not known and this causes the first render of the app after registration to not be aware of the name of the user. If the page gets refreshed, the name would appear, as it's now persisted.

我可以自己将用户数据保持在状态中,但是这失去了使用自动订阅的某些好处.

I could persist the user data in the state myself, but this is losing some of the benefits of using the automatic subscription.

你们有没有解决这个问题的更好方法?

Does any of you solved this problem is a nicer way?

谢谢!

编辑:我还在auth中找到了一个名为 updateCurrentUser 的方法,并且我尝试在更新配置文件完成后调用它.但是,这并不会触发文档中所述的 onAuthStateChanged .

I have also found a method in auth, called updateCurrentUser and I have tried calling it after the update profile has finished. This is however not triggering the onAuthStateChanged, as stated in the docs.

推荐答案

用户配置文件的更新不会导致触发任何身份验证状态侦听器回调.自上次调用该监听器以来,仅当用户新登录或注销时才调用该监听器.如果您需要在更新后重新加载用户的个人资料,则可以调用重新加载()放在用户对象上.

An update of a user profile does not result in any auth state listener callbacks to be triggered. Listeners are only invoked when the user is newly signed in or signed out since the last time the listener was invoked. If you need to reload a user's profile after an update, you can call reload() on the user object.

这篇关于Firebase(Web)-注册后添加用户数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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