【问题标题】:Firebase WEB - Email Verification not being sent. What's wrong with the codeFirebase WEB - 未发送电子邮件验证。代码有什么问题
【发布时间】:2017-08-26 14:30:51
【问题描述】:

好吧,我已经尝试过使用多种方法来触发函数sendEmailVerification()。但没有一个工作成功。文档也没有帮助。

以下是我将使用的部分源代码。请让我知道如何纠正这个问题。

在我的控制台上,我收到以下错误:

TypeError:无法读取 null 的属性“emailVerified” 在 Object.firebase.auth.onAuthStateChanged.firebaseUser [下一个]

btnSignUpWithGoogle.addEventListener('click', e => { var provider = new firebase.auth.GoogleAuthProvider(); firebase.auth().signInWithPopup(provider).then(function(result) { var token = result.credential.accessToken; 变种用户=结果.用户; }).catch(函数(错误){ var errorCode = error.code; var errorMessage = error.message; var email = error.email; var credential = error.credential; console.log(errorCode); }); }); btnLogin.addEventListener('click', e => { 常量电子邮件 = txtEmail.value; const pass = txtPassword.value; const auth = firebase.auth(); const promise = auth.signInWithEmailAndPassword(email, pass); promise.catch(e => console.log(e.message)); txtEmail.value = ""; txtPassword.value = ""; }); btnSignUp.addEventListener('click', e => { 常量电子邮件 = txtEmail.value; const pass = txtPassword.value; const auth = firebase.auth(); const promise = auth.createUserWithEmailAndPassword(email, pass); promise.catch(e => console.log(e.message)); txtEmail.value = ""; txtPassword.value = ""; 常量 emailVerified = firebaseUser.emailVerified; 如果(!emailVerified){ firebase.auth().firebaseUser.sendEmailVerification().then(function(){ alert('请检查您的电子邮件以验证您的帐户。'); }); } 别的 { alert('您的邮箱已通过验证!'); } }); firebase.auth().onAuthStateChanged(firebaseUser => { 如果(火力基地用户){ console.log(firebaseUser); console.log('登录!'); btnLogout.style.visibility = '可见'; } 如果(firebaseUser.emailVerified){ console.log('邮箱已验证'); } 别的 { console.log('邮箱未验证'); firebaseUser.sendEmailVerification(); } });

【问题讨论】:

  • if (firebaseUser.emailVerified) { 应该改为 if (firebaseUser && firebaseUser.emailVerified) {

标签: javascript authentication firebase firebase-authentication


【解决方案1】:

sendEmailVerification() 仅在您将电子邮件地址加载到 firebase 后才有效。

检查以下代码: https://github.com/aqeelsmith/AngularJsData/blob/master/builds/angulardata/js/services/authentication.js - 82号线

【讨论】:

    【解决方案2】:

    我们能看到所有的代码吗?您确定您的 firebase.auth() 声明正确吗?

    您的身份验证应如下所示:

    const firebaseApp = firebase.initializeApp(firebaseConfig, 'Client');
    const firebaseAuth = firebaseApp.auth();
    

    firebaseUser 应该有 emailVerified 密钥。像这样在 if firebaseUser 中移动 if else 语句:

    firebase.auth().onAuthStateChanged(firebaseUser => {
      if (firebaseUser) {
        console.log(firebaseUser);
        console.log('Logged IN!');
        btnLogout.style.visibility = 'visible';
    
        if (firebaseUser.emailVerified) {
          console.log('Email is verified');
        }
        else {
          console.log('Email is not verified');
          firebaseUser.sendEmailVerification();
        }
    
      }
    
    });
    

    console.log(firebaseUser),说什么?

    【讨论】:

      猜你喜欢
      • 2013-07-31
      • 2019-06-24
      • 2018-11-21
      • 1970-01-01
      • 1970-01-01
      • 2017-09-09
      • 2020-08-20
      • 1970-01-01
      • 2018-01-01
      相关资源
      最近更新 更多