gcfabri
gcfabri

Reputation: 574

Reauthenticate Firebase User

I am working on a project with angularfire and I am trying to implement the method to update user password. Due the messed documentation about it, please help me to find a solution to re-authenticate an user. I've already read this 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 service:

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

Console error:

TypeError: Cannot read property 'credential' of undefined

Upvotes: 2

Views: 1673

Answers (1)

gcfabri
gcfabri

Reputation: 574

You can get credential using:

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

instead of:

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

Upvotes: 1

Related Questions