I am trying to implement a check before entering a state using the ui-router resolve property. The goal is to verify if the user is logged in by calling the AuthService function. However, I am not getting any result.
- Angular version: 1.5.8
- angular-ui.router version: 0.2.18
index.route.js
angular.module('app').config(routerConfig);
/** @ngInject */
function routerConfig($stateProvider, $urlRouterProvider, USER_ROLES) {
$stateProvider
.state('dashboard', {
url: "/dashboard",
templateUrl: "app/views/dashboard_2.html",
data: { requiresAuth: true, pageTitle: 'Dashboard', authorizedRoles: ['admin'] } ,
resolve: {
isLoggedin: ['AuthService', function(AuthService) {
return AuthService.isAuthenticated();
}]
}
})
});
auth.service.js
'use strict';
angular
.module('app')
.factory('AuthService', function($log, $http, $q, $cookies){
var authService = {};
// .. login and logout methods...
authService.isAuthenticated = function () {
return !!$cookies.getObject('currentUser');
};
return authService;
});
index.run.js
angular
.module('app')
.run(function($rootScope, $state, $log, AuthService, AUTH_EVENTS) {
$rootScope.$on('$stateChangeStart', function (event, to, toParams, from, fromParams) {
// $log.debug('state change starts');
$log.debug(to.resolve);
});
I have come across information stating that resolve returns a promise object, but my simple function only returns true/false. Could this be the issue? Any suggestions on how to properly handle this situation?