Jasur Kurbanov
Jasur Kurbanov

Reputation: 100

Firebase email verification WITHOUT creating an account in React Native

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.

Upvotes: 0

Views: 351

Answers (1)

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)),
    );
  }, []);

Upvotes: 1

Related Questions