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

9
我正在使用Firebase开发一个Web项目,需要在登录页面处理所有身份验证错误。
为什么我无法像处理其他“auth/something-error”错误代码一样获得“auth/argument-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();
                });
        });
    };
4个回答

8
您需要拥有具有在新RecaptchaVerifier构造函数中指定的ID的div,button或任何元素。请查看以下代码:
window.recaptchaVerifier = new RecaptchaVerifier("sign-in-button", {
  'callback': (response) => {
    console.log("prepared phone auth process");
  }
}, auth);

5

当未向 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);

    });


};

3
我认为这是RecaptchaVerifier的问题。

2
在我的情况下,我需要在initializeAuth函数中添加Dependencies以使用ProvidersEmail & Password。请注意保留HTML标签。
export const auth = () => initializeAuth(app(), {
  persistence: browserSessionPersistence,
  popupRedirectResolver: browserPopupRedirectResolver,
});

如果您需要更多细节,我在这里找到了我的解决方案


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接