I have this authentication service, that checks if the user has a Session stored. and if it get's the info from the api by returning the $http.post():
authService.getUser = function () {
if (!SessionService.get('Auth')) return null;
$userId = SessionService.get('User');
SessionService.set('Auth', false); // reset to false
if ($userId == null) return null;
return $http.get('/api/users/' + $userId)
.success(function (res) {
SessionService.set('Auth', true);
return res;
})
.error(function () {
return null;
});
}
and this is called like following:
AuthenticationService.getUser()
.success(function (res) {
$scope.user = res;
console.log(res);
})
.error(function () {
Materialize.toast('You are not logged in!');
});
but this fails ofcourse when it doesn't have a session stored, because I return null.
So how would you change the service that the return null triggers the error()