重新验证Firebase用户

时间:2016-10-14 19:54:41

标签: firebase

我正在使用angularfire开发一个项目,我正在尝试实现更新用户密码的方法。由于有关它的混乱文档,请帮助我找到重新验证用户的解决方案。我已经阅读了这个stackoverflow question

account.js:

vm.updateUserPassword = function() {
        if (vm.oldPassword && vm.newPassword && vm.confirmNewPassword) {
            if (vm.newPassword === vm.confirmNewPassword) {
                var currentCredential = firebaseAuth.EmailAuthProvider.credential(vm.currentAuth.email, vm.oldPassword);
                vm.currentAuth.reauthenticate(currentCredential)
                    .then(function() {
                        Database.updateUserPassword(vm.newPassword);
                    }, function(error) {
                        console.error('[Account]', error);
                    });
            } else {
                toastr.error('A nova senha não confere');
            }
        } else {
            toastr.error('Preencha todos os campos corretamente');
        }
    };

database.js服务:

vm.updateUserPassword = function(newPassword) {
        firebaseAuth.$updatePassword(newPassword)
            .then(function() {
                console.log('[Database] Password changed successfully!');
            }).catch(function(error) {
                switch (error.code) {
                    case 'auth/requires-recent-login':
                        vm.translationId = 'FIREBASE.AUTH.REQUIRES_RECENT_LOGIN.ERROR_MSG';
                        break;
                    default:
                        vm.translationId = error.message;
                }
                $translate(vm.translationId)
                    .then(function(translated) {
                        toastr.error(translated);
                    }, function(translationId) {                            
                        vm.translationId = translationId;
                    });
            });
    };

控制台错误:

TypeError: Cannot read property 'credential' of undefined

1 个答案:

答案 0 :(得分:1)

您可以使用以下方式获取凭证:

firebase.auth.EmailAuthProvider.credential(user.email, userProvidedPassword);

而不是:

firebase.auth().EmailAuthProvider.credential(user.email, userProvidedPassword);