React Native check user email with sendEmailVerification after createUserWithEmailAndPassword is perform

I have given a task to code about sending email verification to user after they have registered themselves in signup screen which is react native based. Create new user is handle in one js file called AuthProvider.js. In one of the return value of AuthContext.Provider, there is one action which handle create new user which is shown code below and is working fine.

    registerWithEmail: async (userDetails) => {
                        const { email, password } = userDetails;
    
                        return await auth()
                            .createUserWithEmailAndPassword(email, password)
                            .then(() => {
                       //wish to add on the send email verification action here
                                return true;
                            });
                    }

The return true code above is used to do checking in signup screen. If I wish to return the true value only if the condition where verification email is send to them and is clicked. How can I do it and can I have any kind of guidance?

Answer

You can use async-await syntax with `try-catch this way.

registerWithEmail: async (userDetails) => {
  try {
    const { email, password } = userDetails;
    const {user} = await auth().createUserWithEmailAndPassword(email, password)
    await user.sendEmailVerification()
    return true
  } catch (e) {
    console.log(e)
    return false
  }

It’ll return true only when the email is sent. If there’s any error in the function it’ll trigger catch and the function will return false.