如何在Firebase和Vue.js中设置电子邮件验证

时间:2019-06-26 15:01:50

标签: javascript firebase vue.js google-cloud-firestore

我正在尝试根据我在Github上找到的示例构建Vue.js / Firebase身份验证接口(请参阅:https://github.com/aofdev/vuefire-auth)。此代码使用.sendEmailVerification()挂钩,以便在注册帐户时触发验证电子邮件。注册时基于电子邮件的验证似乎可以正常工作。但是,登录也似乎会触发验证电子邮件,我相信是因为未将此代码中的.onAuthStatechanged()挂钩设置为区分登录和登录。基本上,我想对此进行设置验证电子邮件仅在注册时触发,而不在登录时触发。解决此问题的最佳方法是什么?请参阅下面的代码。谢谢!

<template>
  <div>
    <div class="container">
      <input type="email" id="txtEmail" v-model="authInput.txtEmail" placeholder="Email">
      <input type="Password" id="txtPassword" v-model="authInput.txtPassword" placeholder="Password">
    </div>

    <div class="container">
      <button id="btnLogin" v-on:click="Login()">Log in</button>
      <button id="btnSignUp" v-on:click="SignUp()">Sign Up</button>
      <button id="btnLogout" v-on:click="LogOut()" style="display:none">Log out</button>
    </div>
  </div>
</template>
<script>
    import Firebase from 'firebase'
    export default {
        data() {
            return {
                authInput: {
                    txtEmail: '',
                    txtPassword: ''
                }
            }
        },
        methods: {
            Login: function(event) {
                const email = this.authInput.txtEmail;
                const pass = this.authInput.txtPassword;
                const auth = Firebase.auth();
                const promise = auth.signInWithEmailAndPassword(email, pass);
                this.authInput.txtEmail = '';
                this.authInput.txtPassword = '';
                promise.catch(event => console.log(event.message));
            },
            SignUp: function(event) {
                const email = this.authInput.txtEmail;
                const pass = this.authInput.txtPassword;
                const auth = Firebase.auth();
                const promise = auth.createUserWithEmailAndPassword(email, pass);
                this.authInput.txtEmail = '';
                this.authInput.txtPassword = '';
                promise.catch(event => console.log(event.message));
            },
            LogOut: function() {
                Firebase.auth().signOut();
            }
        }
    }
    Firebase.auth().onAuthStateChanged(firebaseUser => {
        if (firebaseUser) {
            firebaseUser.sendEmailVerification().then(function() {
                console.log('send Verification');
            }, function(error) {
                console.log('not send Verification');
            });
            if (firebaseUser.emailVerified == true) {
                console.log('login success');
                document.getElementById('btnLogout').style.display = 'block';
            } else {
                document.getElementById('btnLogout').style.display = 'none';
            }

        } else {
            console.log('not logged in');
            document.getElementById('btnLogout').style.display = 'none';
        }
    })
</script>
<style media="screen">
  .container {
    margin: 10px;
  }
</style>

1 个答案:

答案 0 :(得分:0)

已解决:

我将Firebase.auth()。onAuthStateChanged()分为两个单独的函数,一个触发触发验证电子邮件,另一个在newUser.emailVerified === true时促进登录。这样就可以仅在注册时触发验证电子邮件,从而解决了我的上述问题。随时添加反馈。

<script>
    import Firebase from 'firebase'
    export default {
        data() {
            return {
                authInput: {
                    txtEmail: '',
                    txtPassword: ''
                }
            }
        },
        methods: {
            Login: function(event) {
                const email = this.authInput.txtEmail;
                const pass = this.authInput.txtPassword;
                const auth = Firebase.auth();
                const promise = auth.signInWithEmailAndPassword(email, pass);
                this.authInput.txtEmail = '';
                this.authInput.txtPassword = '';
                promise.catch(event => console.log(event.message));

                auth.onAuthStateChanged(newUser => {
                  if (newUser) {
                      if (newUser.emailVerified == true) {
                          console.log('login success');
                          document.getElementById('btnLogout').style.display = '';
                          document.getElementById('btnLogin').style.display = 'none';
                          document.getElementById('btnSignUp').style.display = 'none';
                          document.getElementById("verifMessage").innerHTML = "You are now logged in!";
                      } else {
                          document.getElementById('btnLogout').style.display = 'none';
                      }
                  } else {
                      console.log('not logged in');
                      document.getElementById('btnLogout').style.display = 'none';
                      document.getElementById('btnLogin').style.display = '';
                      document.getElementById('btnSignUp').style.display = '';
                  }
                })
            },
            SignUp: function(event) {
                const email = this.authInput.txtEmail;
                const pass = this.authInput.txtPassword;
                const auth = Firebase.auth();
                const promise = auth.createUserWithEmailAndPassword(email, pass);
                this.authInput.txtEmail = '';
                this.authInput.txtPassword = '';
                promise.catch(event => console.log(event.message));

                auth.onAuthStateChanged(firebaseUser => {
                    if (firebaseUser) {
                        firebaseUser.sendEmailVerification().then(function() {
                            console.log('send Verification');
                            document.getElementById("verifMessage").innerHTML = "Check your inbox for verification email!";
                        }, function(error) {
                            console.log('not send Verification');
                        });
                    } else {
                        console.log('not logged in');
                        document.getElementById('btnLogout').style.display = 'none';
                    }
                })
            },
            LogOut: function() {
                Firebase.auth().signOut();
                document.getElementById("verifMessage").innerHTML = "You are now logged out!";
            }
        }
    }
</script>