Firebase create user with email, password, display name and photo url

后端 未结 6 1172
死守一世寂寞
死守一世寂寞 2020-12-15 07:17

According to Firebase site, I am using this code to create a new user:

firebase.auth().createUserWithEmailAndPassword(email, password).catch(function(error)          


        
6条回答
  •  旧巷少年郎
    2020-12-15 07:55

    You can use the Firebase Admin SDK in Firebase Function exactly for your purpose, i.e. to fill up other user properties as the user is created:

    const admin = require("firebase-admin");
    
    // Put this code block in your Firebase Function:
    admin.auth().createUser({
            email: email,
            emailVerified: false,
            password: password,
            displayName: `${fname} ${lname}`,
            disabled: false
        })
    

    But creating user with Firebase Admin SDK may give you problem in sending email verification because the promise does not return the User object that has the sendEmailVerification() method. You may eventually need to use the Firebase client API (as shown in your own code) to create the user and update the user profile before sending the email verification:

    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.
    });
    

    It make sense to update the displayName before sending email verification so that the Firebase email template will greet the new user with proper name rather than just Hello (sounds like a spam) when the displayName is not set.

提交回复
热议问题