-1

How do I get the authCheck factory to fire before the "Check Login state of user" function?

I'm trying to check the state of the $rootScope on routing and http requests:

//Global Logout Function
myApp.run(function($rootScope, $http) {
    $rootScope.logout = function() {
        $http.post('/api/auth/logout');
    };
});
//Check Login state of user
myApp.run(function($rootScope, $http, $window) {
    $rootScope.$on('$routeChangeStart', function () {
        $http.get('/api/auth')
        .then(function successCallback(response) {
            $rootScope.logStatus = response.data.data.loggedIn;
            console.log('initial ' + $rootScope.logStatus);
        }, function errorCallback(response) {
            $rootScope.logStatus = response.data.data.loggedIn;
        });
    return $rootScope.logStatus;
    });

});
//Check for authenticated users on http requests (API calls and Routing changes) and redirect to login if logged out
myBirkman.factory('authCheck', ['$rootScope','$window', function($rootScope, $window) {  

var authCheck = {
    'request': function(config) {
        if ($rootScope.logStatus == true) {
            //do nothing
            console.log('redirect ' + $rootScope.logStatus);
        } else if ($rootScope.logStatus == false) {
            $window.location.href = '/login.php';
        }
    },
    'response': function(response) {
return response;
    }
};
return authCheck;
}]);




// Define routing within the app
myApp.config(['$httpProvider', '$routeProvider', function($httpProvider, $routeProvider) {  
$httpProvider.interceptors.push('authCheck');

I've tried to convert the $rootScope element to a constant, but the same issue is cropping up. The factory runs before the run function so the constant isn't updated until after the factory runs.

Christian Hill
  • 378
  • 1
  • 3
  • 17
  • Are you trying to check the status before the auth has returned a response? If not, console.log $rootScope – brianlmerritt May 10 '16 at 17:09
  • Yes, the console.log #2 is just to check to see if the rootScope was available (for troubleshooting). I left it in to indicate where im seeing that the rootScope is returning as undefined. – Christian Hill May 10 '16 at 17:13
  • See http://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call to understand how asynchronous request works. – Thierry May 10 '16 at 17:43
  • How does that help? The http request is returning the data I want perfectly. My issue is making the rootScope persistent. I have modified the code (see edit above) but the problem is that the check for the loggedIn state fires BEFORE the Check login function. – Christian Hill May 10 '16 at 18:58

2 Answers2

0

You cannot be sure of a value to be present if it is populated after a promise is resolved. You will not be getting the correct value of $rootScope.logStatus because its populated only after the $http.get call is completed, which may happen after your factory code has completed execution

Aditya Singh
  • 15,810
  • 15
  • 45
  • 67
  • Thanks Adita, that makes sense. How would you suggest I Approach this specific problem then? I had to separate the logic of the original http call from the interceptor because it was causing a cyclical logic loop. – Christian Hill May 10 '16 at 17:54
  • Use `http interceptors` instead. Refer this: https://docs.angularjs.org/api/ng/service/$http, http://stackoverflow.com/a/28267784/3878940 – Aditya Singh May 10 '16 at 17:58
  • I am using an interceptor. Look at the last line above. – Christian Hill May 10 '16 at 18:04
  • Yeah. You are but use the one with the syntax of `request`, `requestError`, `response` and `responseError` – Aditya Singh May 10 '16 at 20:25
  • My apologies I thought you had missed the last line so I never looked at the link you provided. Thank you very much! I'll post the corrected answer shortly. – Christian Hill May 10 '16 at 21:26
  • so I changed my factory to the suggested formatting and edited the code above,but now all my http requests (routing, API calls) are broken> Im getting "Property of header is undefined" and Property of data is undefined". – Christian Hill May 10 '16 at 22:04
  • Been a long day. I forgot to return the config. Thanks for the point in the right direction! – Christian Hill May 10 '16 at 22:13
0

Many thanks to Aditya. The solution was that my formatting for the interceptor function was incorrect. After reformatting my code worked like a charm. One note, don't forget to pass back both the config in the request and the response in the response so that your requests still function as expected.

Christian Hill
  • 378
  • 1
  • 3
  • 17