如何处理Firebase Web中的auth / argument-error?

时间:2016-11-07 13:45:54

标签: firebase firebase-authentication

我正在使用Firebase开展网络项目,我需要在登录页面上处理所有身份验证错误。

为什么我无法获得error.code等于' auth / argument-error'就像我和别人一样' auth / something-error'错误代码?

e.g。当我有一个登录表单并按下没有填写电子邮件和密码的按钮时,控制台中会出现以下错误:

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();
                });
        });
    };

1 个答案:

答案 0 :(得分:2)

当没有向firebase提供电子邮件或密码并且无法在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);

    });


};