2016-11-07 75 views
0

我正在使用Firebase處理Web項目,並且需要處理登錄頁面上的所有身份驗證錯誤。如何在Firebase網站中處理auth/argument-error?

爲什麼我不能得到error.code等於'auth/argument-error'就像我對別人的'auth/something-error'錯誤代碼所做的一樣?

例如當我有沒有填寫電子郵件地址和密碼登錄窗體然後按下按鈕,在控制檯下面的錯誤被拋出:

enter image description here

我怎樣才能得到這個錯誤對象,並保存在一個變量?

@Frank van Puffelen,請回答。

signin.js

vm.signIn = function() { 
     vm.loading = true; 
     return $q(function(resolve, reject) { 
      firebaseAuth.$signInWithEmailAndPassword(vm.email, vm.password) 
       .then(function(firebaseUser) { 
        console.log('[Firebase] User signed as', firebaseUser); 
        vm.loading = false; 
        resolve(); 
        $state.go('main'); 
       }).catch(function(error) { 
        console.error('[Code]', error.code); 
        console.error('[Message]', error.message); 
        switch (error.code) { 
         case 'auth/argument-error': 
          vm.translationId = 'IT DOENST WORK LIKE ERRORS BELOW'; 
          console.log('IT DOENST WORK LIKE ERRORS BELOW'); 
          break; 
         case 'auth/wrong-password': 
          vm.translationId = 'FIREBASE.AUTH.WRONG_PASSWORD.ERROR_MSG'; 
          break; 
         case 'auth/user-not-found': 
          vm.translationId = 'FIREBASE.AUTH.USER_NOT_FOUND.ERROR_MSG'; 
          break; 
         case 'auth/user-disabled': 
          vm.translationId = 'FIREBASE.AUTH.USER_DISABLED.ERROR_MSG'; 
          break; 
         case 'auth/invalid-email': 
          vm.translationId = 'FIREBASE.AUTH.INVALID_EMAIL.ERROR_MSG'; 
          break; 
         default: 
          vm.loading = false; 
          vm.translationId = error.message; 
        } 
        $translate(vm.translationId) 
         .then(function(translated) { 
          vm.loading = false; 
          toastr.error(translated); 
         }, function(translationId) { 
          // NOTE: Validar quando o catch dispara! 
          vm.translationId = translationId; 
         }); 
        vm.loading = false; 
        reject(); 
       }); 
     }); 
    }; 

回答

2

當不提供電子郵件或密碼火力點和不能在catch塊來處理這種情況。因爲它是一個參數錯誤。下面是代碼,檢查電子郵件和密碼是否存在,然後只調用firebase.auth()

$scope.login = function(data){ 
    $scope.err = null; 
    $scope.busy = true; 
    if(!data || !data.email || !data.password){ 
     $timeout(function(){ 
      $scope.err = { 
       code:'InvalidParms', 
       message: "Please provide correct email." 
      }; 
      $scope.busy = false; 
     },0); 
     return; 
    } 

    firebase.auth().signInWithEmailAndPassword(data.email,data.password).then(function(res){ 
     if(res){          
      $rootScope.user = res; 
      if(res.emailVerified){ 
       $location.path('/'); 
      }else{ 
       $timeout(function(){ 
        $scope.err = { 
         code:'EmailNotVerified', 
         message: "You email is not yet verified. Please verify your email. Click on resend verification mail if you haven't got a verification mail from us." 
        }; 
       },0); 
      } 
      // 
     } 
     $scope.busy = false; 
    }).catch(function(err){ 
     $timeout(function(){ 
      console.log(err); 
      $scope.busy = false; 
     },0); 

    }); 


};