【问题标题】:Send email verification link not working in firebase auth expo发送电子邮件验证链接在 firebase auth expo 中不起作用
【发布时间】:2022-02-05 23:38:38
【问题描述】:

我正在尝试创建一个您只能使用现有电子邮件的注册系统。我正在尝试在他们按下按钮后向电子邮件发送电子邮件验证链接,如果通过验证,则创建用户。这是我的功能:

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.");
    }
  }

顺便说一句,我也收到此错误

TypeError: null 不是对象(评估 'firebase.default.auth().currentUser.sendEmailVerification')

是否有任何其他方法可以验证电子邮件是否存在。我在使用 expo 客户端的 javascript 上。谁能帮帮我吗?谢谢。

【问题讨论】:

    标签: javascript firebase react-native firebase-authentication expo


    【解决方案1】:

    用户必须登录才能请求验证电子邮件。您似乎正在尝试在登录之前验证用户的电子邮件,甚至首先创建他们的帐户,这目前是不可能的。所以流程应该是:

    // 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!")  
    })
    

    您可以在应用程序中使用emailVerified 属性在用户尚未验证电子邮件时提醒他们,并使用安全规则限制他们的访问。 也结帐:

    Firebase Authentication: Verify email before sign up

    Firebase email verification at SignUp

    【讨论】:

    • 谢谢,正在发送邮件,但是发送时,验证立即失败。当我打开验证电子邮件链接时,有什么办法可以验证吗?我尝试调用 reload(),但没有成功。
    • @AnikDey 验证失败是什么意思?你能提供任何细节吗?
    • 发送邮件时提示验证失败。我使用 if 语句来检查用户是否经过验证。如果用户未通过验证,我返回了一条警告说用户未通过验证,如果用户通过验证,我返回了一条提示帐户已创建。但即使我通过链接验证电子邮件,它也没有说用户已通过验证。
    • @AnikDey 最好使用更新的代码和问题的屏幕截图为此发布一个新问题,因为它与原始问题不同。
    • 好的,感谢您的帮助!
    猜你喜欢
    • 1970-01-01
    • 2021-07-10
    • 2019-11-25
    • 2018-11-21
    • 2018-06-02
    • 1970-01-01
    • 2019-01-01
    • 2017-04-29
    • 1970-01-01
    相关资源
    最近更新 更多