I am authenticating against a Firebase. While checking the user credentials I want to show a loading indicator. On successful authentication I redirect the user to a certain state and then want the loading indicator to stop.
I have two problems:
- Sometimes the loading indicator shows for a split second, then goes away. After a moment it comes back and hides again. It is only called once. I cannot really reproduce this one reliably.
- There is a delay between the point when the loading indicator closes and when the desired state is rendered. Is there a way to hide the loading indicator only once a desired state is fully rendered?
What I have:
(I put the loading indicator in a service. I tried putting it directly in the controller which had the same result. So this shouldnāt be a problem.)
Loading Indicator Service
.factory('BusyService', ['$ionicLoading', function($ionicLoading) {
var busy = null;
return {
show: function() {
busy = $ionicLoading.show({
content: '<h1><i class="icon ion-ios7-reloading"></i></h1>',
animation: 'fade-in',
showBackdrop: true,
maxWidth: 93,
showDelay: 0
});
},
hide: function() {
busy.hide();
}
};
}]);
Login method inside Authentication Service
login: function(email, password) {
var deferred = $q.defer();
assertAuth();
simpleAuth.$login('password', {
email: email,
password: password,
rememberMe: true
})
.then(function(user) {
setToken(user.firebaseAuthToken);
deferred.resolve(user);
}, function(error) {
deferred.reject(error);
});
return deferred.promise;
}
Login method inside controller
$scope.login = function() {
BusyService.show();
AuthService.login($scope.authForm.email, $scope.authForm.password)
.then(function() {
$state.go('home');
}, function() {
$scope.authForm.error = MESSAGES.LOGIN_FAILED;
})
.finally(function() {
BusyService.hide();
});
}
};
Any help is greatly appreciated. Thanks in advance.