access.factory('AuthenticationInterceptor', ['$q', '$location', 
'$localStorage', function ($q, $location, $localStorage) {
    return {
        'request': function (config) {
            config.headers = config.headers || {};
            if ($localStorage.token) {
                config.headers.Authorization = $localStorage.token;
            }
            return config;
        },
            'responseError': function (response) {
            if (response.status === 401 || response.status === 403) {
                $location.path('/login');
            }
            return $q.reject(response);
        }
    };
}]);
 
access.config(['$routeProvider', '$httpProvider', function($routeProvider, 
$httpProvider) {

    var checkLoggedIn = function($q, $timeout, $http, $location){
        var deferred = $q.defer();
        $http.get('/loggedin').success(function(user){
            if (user !== '0'){
                deferred.resolve();
            } else {
                alert("You need to log in");
                deferred.reject;
                $location.url('/login');
            }
        });
        return deferred.promise;
    };

    $httpProvider.interceptors.push('AuthenticationInterceptor');
    $routeProvider

    .when('/', {
        templateUrl: 'main.html',
        resolve: {
            loggedin: checkLoggedIn
        }
    })


I want AuthenticationInterceptor to be called before $http.get('/loggedin') 
is, so that a user will be redirected accordingly as to whether they have 
any token on their machine. However, even with this setup, where the user 
has a token, $http.get('/loggedin') always returns that there is no user 
logged in because there is no token in the header, leading me to suspect 
that they are not called as they sequentially should be. How can I get 
around this?

-- 
You received this message because you are subscribed to the Google Groups 
"AngularJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to angular+unsubscr...@googlegroups.com.
To post to this group, send email to angular@googlegroups.com.
Visit this group at https://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.

Reply via email to