简体   繁体   English

发送 email 验证链接在 firebase 验证 expo 中不起作用

[英]Send email verification link not working in firebase auth expo

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.一旦他们按下按钮,我试图将 email 验证链接发送到 email,如果通过验证,则创建用户。 This is my function:这是我的 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')类型错误:null 不是 object(评估 'firebase.default.auth().currentUser.sendEmailVerification')

Is there any other way to verify if the email exists.有没有其他方法可以验证 email 是否存在。 I am on javascript using expo client.我在 javascript 上使用 expo 客户端。 Can anyone please help me?谁能帮帮我吗? Thanks.谢谢。

The user must be logged in to request a verification email.用户必须登录才能请求验证 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.您似乎正在尝试在登录用户之前验证用户的 email,甚至首先创建他们的帐户,这目前是不可能的。 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.如果用户尚未验证 email,您可以在应用中使用emailVerified属性来提醒用户,并使用安全规则限制他们的访问。 Also checkout:还结帐:

Firebase Authentication: Verify email before sign up Firebase 身份验证:在注册前验证 email

Firebase email verification at SignUp Firebase email 在注册时验证

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM