使用Firebase请求发送电子邮件验证

时间:2018-10-09 07:40:41

标签: typescript firebase ionic-framework firebase-realtime-database email-validation

我正在尝试在用户注册时发送电子邮件验证,这是我在provider / user.ts下的代码

onCreate(form: NgForm) {
var user = new User();
user.name = form.value.name;
user.email = form.value.email;
user.contact = form.value.contact;

if(form.value.gender == "male") {
  user.gender = "male";
} else {
  user.gender = "female"
}
let loading = this.mProv.getLoader('Creating your account...', 0);
loading.present();
this.afAuth.auth.createUserWithEmailAndPassword(form.value.email, form.value.password)
.then( res=> {
  let user = firebase.auth().currentUser;
  user.sendEmailVerification();
})
  .then(data => {
    user.id = data.uid;
    this.userProvider.addUser(user)
      .then(_ =>
        loading.dismiss()
      )
      .catch(error => {
        loading.dismiss();
        this.mProv.showAlertOkMessage('Error','Sign up error. Please try again later.');
      });
  })
  .catch(error => {
    loading.dismiss();
    this.mProv.showAlertOkMessage('Error','Sign up error. Please try again later.');
  });
}

我不知道将user.sendEmailVerification放在哪里,当我放下data.uid时,红色下划线

 .then( res=> {
  let user = firebase.auth().currentUser;
  user.sendEmailVerification();
})

将感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

调用.then(data => { ... })时,data的参数是该链的上一阶段返回的参数,但是用user.sendEmailVerification()插入的该阶段不返回任何值。如果您不想等待user.sendEmailVerification()完成,则可以将该代码移至下一个阶段,而不必使用单独的阶段:

this.afAuth.auth.createUserWithEmailAndPassword(form.value.email, form.value.password)
  .then(data => {
    let firebaseUser = firebase.auth().currentUser;
    firebaseUser.sendEmailVerification();
    user.id = data.uid;
    this.userProvider.addUser(user)
      .then(_ =>
        loading.dismiss()
      )
      .catch(error => {
        loading.dismiss();
        this.mProv.showAlertOkMessage('Error','Sign up error. Please try again later.');
      });
  })
  .catch(error => {
    loading.dismiss();
    this.mProv.showAlertOkMessage('Error','Sign up error. Please try again later.');
  });

或者,您可以使用async / await而非then来简化编程风格。这是我在one very basic tutorial中找到的web search