简体   繁体   English

Firebase (web) - 在注册时添加用户数据

[英]Firebase (web) - add user data upon signup

I am having some troubles with the following case:我在以下情况下遇到了一些麻烦:

I am using email login functionality in Firebase Auth.我在 Firebase Auth 中使用 email 登录功能。 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
});

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.问题是,在createUserWithEmailAndPassword完成后,它立即触发onAuthStateChanged ,这是用户在应用程序的本地 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.我可以自己将用户数据保存在 state 中,但这失去了使用自动订阅的一些好处。

Does any of you solved this problem is a nicer way?你们中的任何人解决这个问题是更好的方法吗?

Thanks!谢谢!

Edit: I have also found a method in auth, called updateCurrentUser and I have tried calling it after the update profile has finished.编辑:我还在 auth 中找到了一个名为updateCurrentUser的方法,我尝试在更新配置文件完成后调用它。 This is however not triggering the onAuthStateChanged , as stated in the docs.然而,这不会触发onAuthStateChanged ,如文档中所述。

I think you could try this我想你可以试试这个

firebase.auth().createUserWithEmailAndPassword(email, password).then(function(user) {
    var user = firebase.auth().currentUser;
    user.updateProfile({
        displayName: name
    }).then( ()=>  {
        alert(user.displayName + " " + user.email);
    })   
});

Edit 1:编辑1:

  1. I forgot ")" at the end我忘了最后的“)”
  2. add .then to show alert that the user is updated.添加.then以显示user已更新的警报。 Note: for test purpose disable my firebase.auth().onAuthStateChanged(..) so it won't redirect and the alert will show注意:出于测试目的禁用我的firebase.auth().onAuthStateChanged(..)所以它不会重定向并且alert会显示

Edit 2: Nvm the problem is with onAuthStateChanged it won't get updated编辑 2:Nvm 的问题在于onAuthStateChanged它不会得到更新

An update of a user profile does not result in any auth state listener callbacks to be triggered.用户配置文件的更新不会导致触发任何身份验证 state 侦听器回调。 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.如果您需要在更新后重新加载用户的配置文件,您可以在用户 object 上调用reload()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将数据从用户注册表单发送到firebase数据库 - How to send the data from user signup form to firebase database firebase注册后将用户添加到实时数据库(javascript) - firebase add user to realtime database upon sign up (javascript) 在 React Native Expo 中创建用户时,无法将记录添加到 Firebase - Unable to add a Record into Firebase upon creating User in React Native Expo 当我让用户通过 firebase 注册时,他已通过身份验证但数据未写入 firebase db 中? - When Ever i make user to signup through firebase he is authenticate but data is not written in firebase db? 如何在使用电话号码注册期间将名称和 email 添加到 Firebase 用户帐户? - How do I add name and email to a Firebase user account during signup with phone number? 保存用户在注册firebase上的额外细节 - save user's extra details on signup firebase firebase - 每当用户注册时使用 updateProfile - firebase - Use updateProfile whenever a user signup 如何通过注册电子邮件在Firebase中“命名”数据 - How to “name” data in firebase from signup email 使用 Firebase v9,如何在使用 gmail 登录时将用户添加到用户集合中? - Using Firebase v9, how can I add the user to the user collection upon logging in with gmail? |已解决| 从 Firebase 获取用户身份验证数据并将其添加到 Firebase DB - |SOLVED| Get User Auth data from Firebase & add it to Firebase DB
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM