Search code examples
javascriptfirebaseauthenticationaccountcreation

Firebase account creation trying to set additional info using variables and then getting that information


So, on one document (register.html), I have this code:

firebase.auth().createUserWithEmailAndPassword(email, password).catch(function(error) {
                    // Handle Errors here.
                    var errorCode = error.code;
                    var errorMessage = error.message;
                    // [START_EXCLUDE]
                    if (errorCode == 'auth/weak-password') {
                      weakPass.style="color:red;";
                    }

                            else {
                        unexpected.innerHTML=errorMessage;
                    }
                    console.log(error);

//Handle Account Status
    firebase.auth().onAuthStateChanged(user => {

    if(user) {
            var user = firebase.auth().currentUser;
            var firstname = txtFirst.value;
            var lastname = txtLast.value;
            var School = dropdown.value;

            user.updateProfile({
                firstName: firstname,
                lastName: lastname,
                school: School
            }).then(function() {
                alert("User: " + user + " Firstname: " + user.firstName + " Lastname: " + user.lastName + " School: " + user.school);
                redirect("home.html");
            });


            //After successful login, user will be redirected to home.html
    }

    });

While the user does, in fact, register with an email and a password that is hooked up to the firebase website, and successfully redirects the website to home.html, I don't think it actually adds the data to the users "profile".

I have this code on home.html in attempt to pull the data:

//Handle Account Status
  firebase.auth().onAuthStateChanged(function(user) {
    if (user) {
      // User is signed in.
      var firstname = user.firstName;
      var lastname = user.lastName;
      var school = user.school;

      alert("User: " + user + " Firstname: " + firstname + " Lastname: " + lastname + " School: " + school);
    } else {
      // User is signed out.
    }

  });

However, this doesn't work. The value of user is true, but the alert returns undefined when the alert is called. The alert shows this exactly: "User: [object Object] Firstname: undefined Lastname: undefined School undefined"

What am I doing wrong? Is there a different/better way to store user data using firebase? The only data I storing for users is their first name, last name, and school, as well as the obvious like email and password because that DOES work.

To be honest, I don't know if there's just something wrong with the way I'm setting the data, or pulling the data, or how to test either.


Solution

  • According to the guides,

    You can update a user's basic profile information—the user's display name and profile photo URL—with the updateProfile method.

    So the only values you can change using updateProfile() are displayName and photoUrl. It is not possible to add arbitrary profile fields to a user. Instead, you'll want to store extra information about a user in a database, such as Cloud Firestore. If you'd like more info on Cloud Firestore, this codelab is a great place to start.