如何知道注册邮箱在 Firebase 中是不是经过验证?

Posted

技术标签:

【中文标题】如何知道注册邮箱在 Firebase 中是不是经过验证?【英文标题】:How to know whether the Register Email is verified or not in Firebase?如何知道注册邮箱在 Firebase 中是否经过验证? 【发布时间】:2017-08-26 21:01:20 【问题描述】:
onSubmit(formData) 
if(formData.valid) 
  console.log(formData.value);
  this.af.auth.createUser(
    email: formData.value.email,
    password: formData.value.password
  ).then(
    authState => 
    authState.auth.sendEmailVerification();
    this.router.navigate(['/login'])
  ).catch(
    (err) => 
    console.log(err);
    this.error = err;
  )


在Firebase中,我像上面的代码一样设置SendEmailVerfication,邮件可以正常发送。但是,在我的应用中,没有点击验证邮件的用户和点击的用户没有区别,如何有所作为?

【问题讨论】:

【参考方案1】:

- 如果您已经登录,下面的解决方案可以帮助您检查电子邮件验证状态。

1) 获取当前用户的推荐方法是在 Auth 对象上设置观察者:

 firebase.auth().onAuthStateChanged(authUser => 
      if(authUser.user.emailVerified) //This will return true or false
        console.log('email is verified')
       else
           console.log('email not verified')
       
    )

2)您还可以通过 currentUser 属性获取当前登录的用户。

var user = firebase.auth().currentUser;

if (user.emailVerified) 
  // email is verified.
 else 
  // email is not verified.

- 如果您没有登录,请尝试以下解决方案。

     firebase.auth().signInWithEmailAndPassword(email, password ).then(authUser => 

      if(authUser.user.emailVerified) //This will return true or false
       console.log('email is verified')
      else
        console.log('email not verified')
      
      ).catch(function(error) 

    );

【讨论】:

【参考方案2】:

您可以使用firebase.auth().currentUser.emailVerified

这将返回truefalse

【讨论】:

【参考方案3】:

根据文档,User 对象包含一个 emailVerified 属性。

因此,可以检查 signInWithEmailAndPassword 方法的 promise 解析到的用户 - 或传递给 onAuthStateChanged 方法回调的用户 - 并且可以检查 emailVerified 的值。

【讨论】:

【参考方案4】:

您可以在 firebase 数据库中为您的用户验证状态添加一个属性link

【讨论】:

以上是关于如何知道注册邮箱在 Firebase 中是不是经过验证?的主要内容,如果未能解决你的问题,请参考以下文章

允许用户在使用angular firebase与google注册时,选择用哪个邮箱注册。

如何检查用户是不是在 Firebase 和 Express/Node.js 中经过身份验证?

如何查看邮箱是不是已经注册?

java怎样判断是不是是MSN和QQ邮箱

如何使用 Flutter 在 Firebase 中正确登录和注册?

老司机教你注册电子邮箱后,如何搬迁邮箱!