【问题标题】:Simple email verification in React Native and FirebaseReact Native 和 Firebase 中的简单电子邮件验证
【发布时间】:2021-08-17 20:15:40
【问题描述】:

我在 React Native 中做了一个简单的注册表单。我想通过发送验证码来检查电子邮件是否有效。我阅读了许多类似的问题,但我无法将它们与我的代码结合起来,因为我是 React Native 的新手。任何人都可以在不发布另一个答案的链接的情况下帮助我做到这一点吗?

这是我的注册码:

 export class Register extends Component {
     constructor(props) {
         super(props);
         this.state = { 
             email: '',
             password: '',
             name: '',
             lastname: ''
         }
this.onSignUp = this.onSignUp.bind(this)

     }
onSignUp(){
    if(
        this.state.email != '' &&
        this.state.password != '' &&
        this.state.name != '' 
      ){
    const { email, password, name } = this.state;
    firebase.auth().createUserWithEmailAndPassword(email, password)
    .then((result) => {
        firebase.firestore().collection("Users")
            .doc(firebase.auth().currentUser.uid)
            .set({
                name,
                email
            })
        console.log(result)
    })
    .catch((error) => {
        console.log(error)
    })
}
else{
    alert("Please fill the empty spaces!");
}

}

    render() { form body } 

【问题讨论】:

    标签: firebase react-native firebase-authentication


    【解决方案1】:

    在创建帐户之前,您无法仅使用 Client SDK 验证用户电子邮件。如果您需要这样做,则必须使用 Cloud Functions(或您自己的服务器)以及第三方电子邮件发送服务并实现您自己的逻辑。

    但是,您可以通过向用户发送这样的验证电子邮件来验证用户的电子邮件:

    //new user created
    var user = firebase.auth().currentUser;
    
    user.sendEmailVerification().then(function() {
      // Email sent.
    }).catch(function(error) {
      // An error happened.
    });
    

    这将向您的用户发送一封包含验证链接的电子邮件。此后,您可以使用emailVerified 属性检查用户是否已验证其电子邮件以限制对以下数据的访问:

    if (user.emailVerified) { 
      // process 
    } else {
      alert("Please verify your email")
    }
    

    在您的情况下,代码如下所示:

    const { email, password, name } = this.state;
        firebase.auth().createUserWithEmailAndPassword(email, password)
        .then(async (result) => {
            await firebase.firestore().collection("Users")
                .doc(firebase.auth().currentUser.uid)
                .set({
                    name,
                    email
                })
            await user.sendEmailVerification()
            console.log("Verification Email Sent")
        })
    

    【讨论】:

    • 非常愚蠢的问题,我怎样才能把这个逻辑放在我的代码中?具体在哪里?
    • @CliAd 我已经更新了代码。如果答案对您有帮助,您可以接受/支持它,以便其他人知道它已解决:)
    猜你喜欢
    • 2020-09-08
    • 1970-01-01
    • 2021-10-10
    • 2022-12-04
    • 2020-12-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多