I'm trying to build simple loggin with angular and rest api and I've got strange problem with checking whether user is logged in login form controller.
Here is my code:
everytime I refresh my app it loads current logged user
angular.module('myApp').run(['security', function(security) {
security.requestCurrentUser();
}]);
security module is something like this (simplefied, inspired by angular-app)
angular.module('security').factory('security', ['SERVER_CONFIG', '$http', '$q', '$location', 'securityRetryQueue',
function (SERVER_CONFIG, $http, $q, $location, queue) {
var service = {
currentUser : null,
isAuthenticated : function () {
return !!service.currentUser;
},
login :
....
},
logout :
....
},
requestCurrentUser :
...
}
};
return service;
}]);
so it holds data about current user and using isAuthenticated you can find out wheter user is logged in or not
angular.module('security.login').controller('LoginFormCtrl', ['$scope', 'security', function ($scope, security) {
console.log(security)
console.log(security.currentUser)
console.log(security.isAuthenticated())
}]);
console.log(security) returns object where property user is filled with user data so method isAuthenticated returns true
but here comes the strange thing:
security.user returns null and security.isAuthenticated() returns false and I don't understand why is user null... I need it for redirecting from loggin page when accessed and user is already logged in. I know angular app uses modal for this so it would solve my problem, but I don't want to use modal...
If anyone can explain what I'm doing wrong I would be delighted...
Thanks a lot Blažek