How do I set the displayName of Firebase user?

无人久伴 提交于 2020-03-17 10:28:11

问题


Bear with me I am new to Google Firebase, sorry for my stupidity

According to the JS Auth documentation on the Firebase website, it only shows how to get the displayName and how to update displayName. So I tried to update it. But it is sort of not logical, because how can you update something without creating it.

So my question here is, how can I set displayName of user during registeration?

function createUser(email, password) {
    firebase.auth().createUserWithEmailAndPassword(email, password).catch(function (error) {
        error.message.replace(".", "");
        alert(error.message + " (" + error.code + ")");
        document.getElementById("password").value = "";
    });
    if (firebase.auth().currentUser != null) {
        firebase.auth().currentUser.updateProfile({
            displayName: document.getElementById("name").value
        }).then(function () {
            console.log("Updated");
        }, function (error) {
            console.log("Error happened");
        });
    }
}

I have already tried this and it has been proven not to work...

Sincerely, Farouk


回答1:


You have to chain the request:

firebase.auth().createUserWithEmailAndPassword(email, password)
.then(function(result) {
  return result.user.updateProfile({
    displayName: document.getElementById("name").value
  })
}).catch(function(error) {
  console.log(error);
});`



回答2:


I could´t use ({displayName: name}) directly (sintaxe error in editor). Then, I found another way:

UserUpdateInfo updateInfo = UserUpdateInfo();
updateInfo.displayName = name;
result.user.updateProfile(updateInfo);

This is in Dart (I am using Firebase with Flutter).



来源:https://stackoverflow.com/questions/40389946/how-do-i-set-the-displayname-of-firebase-user

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!