0
votes

I have question regarding email verification in Firebase. Solution to my problem I found here. But when I convert code to react native. It is not working as expected.

My function

const onSignIn = () => {

auth()
  .signInAnonymously()
  .then(() => {
    console.log('User signed in anonymously');

    auth()
      .onAuthStateChanged((user) => {
        user.updateEmail('[email protected]');
        user.sendEmailVerification();
      })
      .catch(function (error) {
        console.log('error', error);
      });
  })
  .catch((error) => {
    if (error.code === 'auth/operation-not-allowed') {
      console.log('Enable anonymous in your firebase console.');
    }
    console.error(error);
  });

navigation.push('SignUpConfirm');

};

Basically what I want to achieve is. When user enters to app, I need to show only email input. Once user enters email address, Firebase should send confirmation code to the email user provided.

1
what is the error ?. Both calls are async so if you not wait for updateEmail to finish, sendEmailVerification will throw an error.anthony willis muñoz
1) how did you know that it is async ? 2) My email is updating, but sendEmailVerification is not working. 3) I will see logs for errors thanJasur Kurbanov

1 Answers

1
votes

You should read the docs https://rnfirebase.io/reference/auth/user#updateEmail with the code below should works. if not print what error you got.

Sign In Screen

const onSignIn = () => {

auth()
  .signInAnonymously()
  .then(() => {
    console.log('User signed in anonymously');
  .catch((error) => {
    if (error.code === 'auth/operation-not-allowed') {
      console.log('Enable anonymous in your firebase console.');
    }
    console.error(error);
  });

navigation.push('SignUpConfirm');

};

SignUpConfirm Screen

useEffect(() => {
    auth().onAuthStateChanged((userIs) =>
      userIs
        .updateEmail('[email protected]')
        .then(() =>
          userIs
            .sendEmailVerification()
            .then(() => console.log('email verificiation sent')),
        )
        .catch((err) => console.log('This is', err)),
    );
  }, []);