0

I have a react app with which I want to handle authentication with firebase.

My code successfully signs up and logs in but i am trying to add extra information on sign up but i have not been successful. I have tried answers [here]: Firebase v3 updateProfile Method and [here]: Firebase user.updateProfile({...}) not working in React App

But they don't seem to work. Below is my code

const SignUp = ({ history }) => {
  const handleSignUp = useCallback(
    async event => {
      event.preventDefault();
      const { email, password } = event.target.elements;
      try {
        let cred = await app
          .auth()
          .createUserWithEmailAndPassword(email.value, password.value);
        await cred.user.updateProfile({
          displayName: 'hello'
        });

        history.push('/');
      } catch (error) {
        console.log(error);
      }
    },
    [history]
  );

Please how do i fix this because currently on the email and username sets? Thanks

1 Answer 1

2

In order to change user profile you should use firebase.auth().onAuthStateChanged() function, as follows:

firebase.auth().onAuthStateChanged(function(user) {
  if (user) {
    // User is signed in.
  } else {
    // No user is signed in.
  }
});

Then you can get others user properties. Here you can find all info you need. https://firebase.google.com/docs/auth/web/manage-users. Hope it helps.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.