I'm struggling with AngularJS and access to connected user info here. My goal is to save the info of the connected user in a Service, so that is available throughout the app. So this is how I do it:
The login function is exposed through a Login service, and its code is the following:
(function(){
var $loginService = angular.module("RockMyTask.LoginService", [
"satellizer",
"RockMyTask.ApiService"
]);
/**
* Login service
*
* function login(identifier, password, after)
* Logs the given user. Global data about the user are registered in the root scope.
* @param identifier The user's identifier (either email or password)
* @param password The user's password
* @param success (optional) A callback to execute when the user is successfully logged in. This callback is passed
* the response object.
* @param failure (optional) A callback to execute when the log in fails. This callback is passed the response
* object.
*
* function restore()
* If the user was already logged in, restore the user data in the rootScope.
*
* function connectedUser()
* Return the data of the currently connected user. If the user is not connected an empty object is returned.
* If the user data hasn't be fetched yet, then an empty object is returned and this object will be filled with
* the user data as soon as it is received.
*/
$loginService.factory("Login", ["$auth", "$rootScope", "User",
function($auth, $rootScope, User) {
var obj = {};
var connectedUser = {};
obj.logout = function() {
$auth.logout();
//Authorization.clear();
connectedUser = null;
};
obj.login = function(identifier, password, success, failure) {
var credentials = {
user_identifier: identifier,
password: password
};
$auth.login(credentials).then(function(auth_response) {
// fetch user data (with both spectator and rockstar flags set to true)
User.me(true, true).then(function(response) {
connectedUser = response.data;
//TODO add check for DEBUG_MODE
console.log("Received connected user data and stored it in connectedUser var");
}, function() {
connectedUser = {};
});
// execute provided success callback
if (_.isFunction(success)) {
success(auth_response);
}
}, function(response) {
connectedUser = null;
// execute provided failure callback
if (_.isFunction(failure)) {
failure(response);
}
});
};
obj.restore = function() {
if ($auth.isAuthenticated()) {
User.me(true, true).then(function(response) {
connectedUser = response.data;
}, function() {
connectedUser = null;
});
} else {
connectedUser = null;
}
};
obj.getConnectedUser = function(){
if($auth.isAuthenticated && !connectedUser){
User.me(true, true).then(function(response) {
connectedUser = response.data;
}, function() {
connectedUser = null;
});
}
return connectedUser;
};
obj.updateUserInfo = function(user){
connectedUser = user;
};
obj.isConnectedUserRockstar = function(){
return connectedUser.rocker != null;
};
obj.isConnectedUserSpectator = function(){
return connectedUser.spectator != null;
};
return obj;
}]);
})();
As you may observe in the code, upon successful login, I launch an HTTP request (User.me(true, ture)
) which then stores the returned data (representing info about connected user) in an ad hoc variable of the service, connectedUser
.
To access the connected user info from any point of the application, I've implemented a function in the Login service called getConnectedUser
, whose code is reported here:
obj.getConnectedUser = function(){
if($auth.isAuthenticated && !connectedUser){
User.me(true, true).then(function(response) {
connectedUser = response.data;
}, function() {
connectedUser = null;
});
}
return connectedUser;
};
This function checks if the user is indeed authenticated and if the variable connectedUser has been already correctly populated and if not it triggers again the HTTP function to retrieve the data (otherwise it simply returns the connectedUser
var).
Now, my problem is the following. The page I redirect the user to, after successful login, uses a controller, to which I've passed the Login service. The controller tries to retrieve the connected user info but the result is a null pointer. What am I doing wrong??
The code in the controller (just in case even if very simple):
$scope.user = Login.getConnectedUser();
EDIT 1 Looking at the console in the browser I see that the HTTP request has success and the returned data is:
{"id":3,"email":"iCartwright@hotmail.com","name":"Michele","surname":"Imperiali","locale":"it_IT","picture":"https:\/\/storage.rockmytask.it\/users\/profile_image\/3.jpeg","birthday":"1982-06-23","gender":"M","country":"IT","province":"Lombardia","city":"Milano","zip":"20100","street":"190 Grover Expressway","mobile_phone":"(437)924-8282","home_phone":"239-250-3166x783","username":"spectator","validated":true,"timezone":"Europe\/Rome","created_at":"2016-04-01T12:50:53+0000","rocker":null,"spectator":{"description":"I need help with my garden !"}}
As previously stated, when I try to access on the variables of $scope.user
in the controller I get a null pointer exception.