Before processing any request, I always verify the user's authorization. Here is the factory code that handles this:
(function()
{
angular.module('employeeApp').factory('authenticationFactory', authenticationFactory);
function authenticationFactory($cookieStore,$cookies,requestFactory,$location,GLOBALS,constants,$q)
{
var factory = {};
factory.validateUser = function()
{
var vm = this;
vm.deferred = $q.defer();
if($location.path() != '/')
{
var api_token = factory.getToken();
factory.isValidToken(api_token).then(function(response) {
if (response.status != 200) {
$location.path('/');
}
data = {"api_token": api_token};
return requestFactory.post(GLOBALS.url + 'show/employee/' + $cookies.get('employeeid'), data)
.then(function (response) {
vm.deferred.resolve(response.data);
console.log(vm.deferred);
return vm.deferred.promise;
}, function(response) {
vm.deferred.reject(response);
return vm.deferred.promise;
});
});
}
}
return factory;
}
})()
When I output vm.deferred
using console.log, it shows as an object.
However, when I call
console.log(authenticationFactory.validateUser());
in my routes
file, the console displays empty
.
(function () {
angular.module('employeeApp').config(routeModule).run(run);
routeModule.$inject = ['$routeProvider'];
function routeModule($routeProvider)
{
$routeProvider.when('/', {
templateUrl: '../views/login.html',
controller: 'authenticationController',
controllerAs: 'authenticationCtrl'
})
.when('/home', {
templateUrl: '../views/index.html',
controller: 'homeController',
controllerAs: 'homeCtrl',
resolve: {
message: function(authenticationFactory){
return authenticationFactory.validateUser();
}
}
})
.when('/werknemer/:id', {
templateUrl: '../views/employee/employee.html',
controller: 'employeeController',
controllerAs: 'employeeCtrl'
})
.otherwise({
redirectTo: '/'
});
}
function run(authenticationFactory)
{
console.log(authenticationFactory.validateUser());
}
})();
I've been stuck on this for hours now, any help would be greatly appreciated!