1

I am trying to create a sign-up system where you can only use existing emails. I am trying to send email verification link to the email once they have pressed a button, and if it is verified, then the user is created. This is my function:

userSignUp = (emailID,password,confirmPassword) => {
    if(password !== confirmPassword) {
        return alert("Password does not match. \n Check your password.")
    }  
    else if(this.state.firstName !== '' &&
            this.state.lastName !== ''  &&
            this.state.emailID !== '' &&
            this.state.password !== ''){
        
        alert('A mail has been sent to your email account. Verify it and you will be able to login.');

        return firebase.auth().currentUser.sendEmailVerification()
        .then(() => {
            if(authUser.user.emailVerified){ //This will return true or false
                firebase.auth().createUserWithEmailAndPassword(emailID, password)
                .then(()=>{
                    //console.log(this.state)
                  db.collection("Users").add({
                      'firstName': this.state.firstName,
                      'lastName': this.state.lastName,
                      'contact': this.state.contact,
                      'emailID': this.state.emailID,
                      'password': this.state.password,
                      'points': 0,
                      'description': ''
                  })
                  return alert('Account has been created. You can now login.');
                })
                .catch((error) => {
                  // Handle Errors here.
                  var errorCode = error.code;
                  var errorMessage = error.message;
                  return alert(errorMessage);
                });
            } else {
                 return alert('Verification failed. Failed to create account.')
            }
        });
      
    }
    else if(this.state.firstName === ''){
        return alert("Please enter your first name.");
    }
    else if(this.state.lastName === ''){
        return alert("Please enter your last name.");
    }
    else if(this.state.emailID === ''){
        return alert("Please enter your email address.");
    }
    else if(this.state.password === ''){
        return alert("Please enter your password.");
    }
  }

Btw I also get this error

TypeError: null is not an object (evaluating 'firebase.default.auth().currentUser.sendEmailVerification')

Is there any other way to verify if the email exists. I am on javascript using expo client. Can anyone please help me? Thanks.

Anik Dey
  • 54
  • 7

1 Answers1

2

The user must be logged in to request a verification email. It seems you are trying to verify user's email before logging them in or even creating their account at first place which is not possible at the moment. So the flow should be:

// 1. Create user account / log user in
firebase.auth().createUserWithEmailAndPassword(emailID, password).then(async ({user}) => {
  // 2. Send verification email
  await user.sendEmailVerification()
  console.log("Verification email sent!")  
})

You can use emailVerified property in app to alert user if they have not verified email yet and restrict their access using security rules. Also checkout:

Firebase Authentication: Verify email before sign up

Firebase email verification at SignUp

Dharmaraj
  • 47,845
  • 8
  • 52
  • 84
  • Thanks, it is sending an email, but when it sends it, the verification immediately fails. Is there any way to make it when I open the verify email link, it verifies? I tried to call reload(), but that didn't work. – Anik Dey Feb 06 '22 at 16:38
  • @AnikDey what do you mean by verification fails? Could you provide any details? – Dharmaraj Feb 06 '22 at 16:43
  • When it sends email, it says verification failed. I used if statements to check whether the user is verified. I returned an alert saying user isn't verified if user isn't verified, and an alert that says account created if user is verified. But even when I verify email through the link, it doesn't say that the user is verified. – Anik Dey Feb 06 '22 at 16:46
  • @AnikDey it might be better to post a new question for this with updated code and screenshots of the issue since it's different than original issue. – Dharmaraj Feb 06 '22 at 16:48
  • Ok sure, thanks for the help! – Anik Dey Feb 06 '22 at 16:50