I am facing an issue with storing the currentUser object in a factory to make it accessible throughout my app. Despite ensuring that the user object is being received server side, whenever I call CurrentUserFactory.GetCurrentUser(), it returns null instead of the expected data.
angular.module('myWebApp.services')
.factory('CurrentUserFactory', ['SettingsFactory', '$http', function(SettingsFactory, $http) {
var CurrentUserFactory = {};
var currentUser = null;
CurrentUserFactory.GetCurrentUser = function() {
if (!currentUser) {
$http.get(SettingsFactory.myAPIUrl + 'api/users/current', { withCredentials: true }).then(function (response) {
currentUser = response.data;
});
}
return currentUser;
}
return CurrentUserFactory;
}
]);