0
votes

I am finding it difficult to save displayName and photoURL of user to firebase during sign-up in my react native project.

I am using the docs(https://firebase.google.com/docs/reference/js/firebase.User.html#updateprofile) as a guide.

See below my code:

  onRegister = () => {
  firebase.auth().createUserWithEmailAndPassword( this.state.typedEmail, this.state.typedPassword)
  .then((userCredentials) => {
    if(userCredentials.user) {
      console.warn(userCredentials.user)
      userCredentials.user.updateProfile({
        displayName: "Jane Q. User",
        photoURL: "https://example.com/jane-q-user/profile.jpg"
      })
      .then(() => {
        this.setState({ user: userCredentials.user })
        console.warn("yo:", this.state.user)
      })
    }
  }).catch((error) => {
      console.warn(`error:, ${error}`)
  })}

With the above function I can save the email and password but the displayName and photoURL still returns null.

Please help.

1

1 Answers

1
votes

You could try to reload then get current user

async function updateProfile() {
  await firebase.auth().currentUser.updateProfile({
        displayName: "Jane Q. User",
        photoURL: "https://example.com/jane-q-user/profile.jpg"
      });
  await firebase.auth().currentUser.reload();
  console.log(firebase.auth().currentUser.displayName); <-- check if it is updated
}

UPDATE

onRegister = async () => {
  try {
    const userCredentials = await firebase
      .auth()
      .createUserWithEmailAndPassword(this.state.typedEmail, this.state.typedPassword);
    if (userCredentials.user) {
      console.warn(userCredentials.user);
      await userCredentials.user.updateProfile({
        displayName: 'Jane Q. User',
        photoURL: 'https://example.com/jane-q-user/profile.jpg',
      });
      await userCredentials.user.reload();
      this.setState({ user: firebase.auth().currentUser });
      console.warn('yo:', this.state.user);
    }
  } catch (error) {
    console.warn(`error:, ${error}`);
  }
};