1

I have been attempting to create a script that grabs session data from a PHP API using Angular JS to create authentication.

I have created a factory called User and in my loginController I use User.Initialise() to check whether the session in PHP has a user_id attached to it.

When using User.Initialise() in my loginController (bare in mine my session has a user attached to it) it will use $location.path("/dashboard") to change the route to the dashboard. The dashboard route has a controller that has the variable $scope.UserID which is assigned using User.Session.user_id, however, when I attempt to call User.Session it returns undefined, even though User.Initialise(); assigns it in the loginController.

Can anybody shed some light on this?

var $Gladium = angular.module("Gladium", ["ngRoute", "ngAnimate", "ngSanitize"]);

$Gladium.config(function($routeProvider, $locationProvider){

    $routeProvider.when("/",{
        templateUrl: "pages/login.html",
        controller: "loginController"
    }).when("/dashboard",{
        templateUrl: "pages/dashboard.html",
        controller: "dashboardController"
    }).when("/error/:error_code", {
        templateUrl: "pages/system_error.html",
        controller: "errorController"
    });

});

/* Controllers */
$Gladium.controller("dashboardController", function($scope, User){
    console.log("Services:");
    console.log(User.Session);

});
$Gladium.controller("loginController", function($scope, $location, User){

    User.Initialise().then(function(){
        $scope.Authenticate = true;
        if(User.loggedIn()){
            $location.path("/dashboard");
        }
    });

    /* Variables */
    $scope.Email;
    $scope.Password;

    $scope.Login = function(){
        if(User.logIn($scope.Email, $scope.Password)){
            $location.path("/dashboard");
            return true;
        }
    }



});
$Gladium.controller("errorController", function($scope, $routeParams){

    $scope.Errors = {
        "request": "We couldn't process that request at this time. Please try again later.",
        "unknown": "An unknown error occurred if this is persistant please contact technical support."
    };

    $scope.currentError =  $scope.Errors[$routeParams["error_code"]];

});

/* Factories */
$Gladium.factory("User", function($http, $location){

    var Session;
    var Error;

    return {

        Initialise: function(){
            return $http.get("core.php?do=getSession").then(function(requestData){

                if(requestData.data["requestStatus"] == 1){
                    Session = requestData.data.data;
                }else{
                    $location.path("/error/request");
                    return false;
                }

            });
        },


        loggedIn: function(){
            if(Session["user_id"] != 0){
                return true;
            }else{
                return false;
            }

        },

        logOut: function(){
            if(Session["user_id"] != 0 ){
                $http.post("core.php",{
                    do: "logout"
                }).then(function(requestData){
                    if(requestData.data["requestStatus"] == 1){

                    }else{

                    }
                });
            }else{
                console.warn("There is no user session to logout.");
            }
        },

        logIn: function(Email, Password){
            $http.post("core.php",{
                do: "login",
                email: Email,
                password: Password
            }).then(function(requestData){
                if(requestData.data["requestStatus"] == 1){

                    Data = requestData.data;

                    if(Data["actionStatus"] == 1){
                        Initialise();
                    }else{
                        Error = Data["Error"];
                        return false;
                    }

                }else{
                    $location.path("/error/request");
                    return false;
                }

                $location.path("/error/unknown");
                return false;
            });
        }

    }

});
4
  • Add 'User' refernce in controller $Gladium.controller("loginController", ['$scope', '$location', 'User',function($scope, $location, User){ Commented Dec 22, 2016 at 15:48
  • Whats the difference between that and injecting it? Could you elaborate? Commented Dec 22, 2016 at 15:50
  • changing to the syntax that @NagaveerGowda suggested would only be to make the app minification safe. If you aren't minifying the scripts, the syntax you are using here is fine, though somewhat substandard. it definitely wouldn't affect your error, but it could create a different kind of error, if the app is minified.... Commented Dec 22, 2016 at 16:04
  • That has nothing to do with my question. I'm not minifying anything. Commented Dec 22, 2016 at 16:12

1 Answer 1

1

I think that u just forget to return the Session variable which should be a property of User Service

....
$Gladium.factory("User", function($http, $location){

var Session;
var Error;

return {
    // return the Session so it can be accessed via User.Session, or it is a variable in private closure
    Session:Session
    Initialise: function(){
        return $http.get("core.php?do=getSession").then(function(requestData){

            if(requestData.data["requestStatus"] == 1){
                Session = requestData.data.data;
            }else{
                $location.path("/error/request");
                return false;
            }

        });
    },
....

UPDATE

Sorry the change above won't solve your problem since you are assigning the Session closure variable, which will not change User.Session.In this way it still remains undefined.

There are several ways for you to solve this problem.

One i think that is the most simple is to keep the Session private and access it via a get function User.getSession().

$Gladium.factory("User", function($http, $location){

var Session;
var Error;

return {
// use get function to get Session
getSession:function(){
  return Session;
},
Initialise: function(){
    return $http.get("core.php?do=getSession").then(function(requestData){

        if(requestData.data["requestStatus"] == 1){
            Session = requestData.data.data;
        }else{
            $location.path("/error/request");
            return false;
        }

    });
},
....

In this way u can access your Session by User.getSession().

Sign up to request clarification or add additional context in comments.

1 Comment

Still undefined, sorry.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.