如何知道Firebase中是否验证了注册电子邮件?

时间:2017-04-01 17:53:02

标签: firebase firebase-authentication angularfire2

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,就像上面的代码一样,电子邮件可以正常发送。但是,在我的应用中,未点击验证电子邮件的用户与点击的用户之间没有区别,如何有所作为?

4 个答案:

答案 0 :(得分:13)

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

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

答案 1 :(得分:4)

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

这将返回truefalse

答案 2 :(得分: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) {

    });

答案 3 :(得分:0)

您可以在firebase数据库中添加用户验证状态link

的属性
相关问题