简体   繁体   English

如何使用 javascript 在 firebase 中添加用户的额外信息,例如显示名称?

[英]How to add user's extra information such as display name in firebase using javascript?

Using createUserWithEmailAndPassword function of firebase I am able to sign users up but how to add extra information such as display name and picture URL?使用 firebase 的createUserWithEmailAndPassword function 我可以注册用户,但如何添加额外信息,例如显示名称和图片 ZE6B391A8D2C4D45902A23A8B6585703D This is what I have done.这就是我所做的。

const signup = document.querySelector('#signup-form');

signup.addEventListener('submit', e=>{
    e.preventDefault();
    //get user info
    const first_name = signup['firstname'].value;
    const last_name = signup['lastname'].value;
    const email = signup['email1'].value;
    const password = signup['pswd1'].value;
    //sigup the user
    firebase.auth().createUserWithEmailAndPassword(email, password).then(()=>{
      firebase.auth().onAuthStateChanged(function(user){
        if(user){
          user.updateProfile({
            displayName: first_name 
          })
        }
      })
      signup.reset();
    }).catch(function(error) {
        // Handle Errors here.
        var errorCode = error.code;
        var errorMessage = error.message;
        window.alert("Error: " + errorMessage);
        // ...
      });    
});

Once you have created the user, you can access user object provided by firebase.创建用户后,即可访问firebase提供的用户object。 Using its updateProfile function, you can pass an object containing the properties you want the user object to have, like the code below:使用它的 updateProfile function,您可以传递包含您希望用户 object 拥有的属性的 object,如下面的代码:

 var user = firebase.auth().currentUser;

user.updateProfile({
  displayName: "Jane Q. User",
  photoURL: "https://example.com/jane-q-user/profile.jpg"
}).then(function() {
  // Update successful.
}).catch(function(error) {
  // An error happened.
});

This worked for me.这对我有用。

firebase.auth().createUserWithEmailAndPassword(email, password).then(cred=>{
      cred.user.updateProfile({
        displayName: first_name + " " + last_name
      })
    })
    .catch(function(error) {
        // Handle Errors here.
        var errorCode = error.code;
        var errorMessage = error.message;
        window.alert("Error: " + errorMessage);
        // ...
    });

暂无
暂无

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

相关问题 我可以使用此代码创建 firebase 用户,但如何在默认 firebase 用户表中添加显示名称、电话号码和其他信息? 谢谢 - I can create firebase user using this code, but how can I add display Name, phone Number and other information in default firebase user table? Thanks 如何使用 JavaScript 显示用户输入的信息? - How to display user entered information using JavaScript? 如何在javascript中很好地显示用户的时区信息 - How to display user's time zone information nicely in javascript 如何在Firebase上添加Child,然后获取并显示信息? - How to Add Child on Firebase and then Get it and Display the information? 如何在JavaScript中显示用户时区的缩写名称(不是GMT偏移)? - How to display the abbreviated name of the user's timezone (not GMT offset) in JavaScript? 如何使用JavaScript SDK获取Facebook用户的朋友信息 - How to get Facebook user's friends information using JavaScript SDK 如何通过 Firebase 认证获取用户名以外的用户信息 - how to get user information other than user name with Firebase Authentification 如何从firebase检索信息并使用javascript将其显示在HTML表格中 - How can I retrieve information from firebase and display it in a HTML table using javascript 格式化javascript对象以不显示其他语言环境信息 - Formating a javascript object not to display extra locale information 如何在猫鼬数据库中添加用户信息? - how to add user's information in mongoose database?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM