如何在signUp方法上显示Firebase Auth错误?

时间:2018-09-27 16:04:41

标签: javascript html firebase firebase-authentication

我无法找到一种显示用户是否成功创建的方法。 我尝试使用if(errorToast!= null),但是没有用。

  firebase.auth().createUserWithEmailAndPassword(userEmail, userPass).catch(function(error) {
   var errorToast = error;
   M.toast({html: error});  
   if (errorToast === 'auth/account-exists-with-different-credential') {
     M.toast({html: 'Outra conta já existe com esse Email'});  
   }
   else if (errorToast == 'auth/weak-password') {
     M.toast({html: 'Senha muito fraca'});  
   }
   else{
     M.toast({html: 'Cadastro realizado com sucesso'});
     sendEmailVerification();
   }

1 个答案:

答案 0 :(得分:1)

createUserWithEmailAndPassword返回包含firebase.auth.UserCredential的Promise,因此您应使用then(),如下所示:

firebase.auth().createUserWithEmailAndPassword(userEmail, userPass)
.then(function(userCredential) {
  // SUCCESS!! -> Do whatever you want
  // e.g. print the user to the console
  console.log(userCredential.user);
})
.catch(function(error) {
   // ERROR!! -> show the error, as you are already doing
   var errorToast = error;
   M.toast({html: error});  
   if (errorToast === 'auth/account-exists-with-different-credential') {
     M.toast({html: 'Outra conta já existe com esse Email'});  
   }
   else if (errorToast == 'auth/weak-password') {
     M.toast({html: 'Senha muito fraca'});  
   }
})

有关承诺(和then()方法)如何工作的更多说明,请参见以下页面:https://developers.google.com/web/ilt/pwa/working-with-promiseshttps://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise