状态解决不在ui-router中工作

时间:2015-07-27 19:28:30

标签: javascript angularjs web-applications angular-ui-router angularjs-routing

我正在使用this tutorial找出我正在处理的网络应用的身份验证系统。我使用ui-router的StateProvider并解析系统,如果用户试图访问需要身份验证的其中一个页面,则将用户重新路由到主页。一切似乎都在起作用,除了解决方案部分似乎没有实际工作 - 即我的身份验证返回一个被拒绝的承诺,但页面加载正常,尽管事实上应该有某种错误,因为这个。我做错了什么?

app.states.js

angular
    .module('app')
    .config(routeConfig);

/** @ngInject */
function routeConfig($stateProvider, $urlRouterProvider) {
  $urlRouterProvider.otherwise('/');

  // checks if user is logged in or not
  // passes back rejected promise if not, resolved promise if true
  function authenticated(authFactory, $q) {
    var deferred = $q.defer();
    authFactory.authenticate()
      .then(function(authenticate) {
        if (authenticate.data === 'true') {
          deferred.resolve();
        } else {
          deferred.reject();
        }
      });
    return deferred.promise;
  }

  // every new state that should include a sidebar must have it as a view
  $stateProvider
    .state('dashboard', {
          url: '/dashboard/',
          views: {
            'sidebar': {
              templateUrl: 'app/components/navbar/sidebar.html',
              controller: 'SidebarController as vm'
            },
            'content': {
              templateUrl: 'app/components/authenticated/dashboard.html',
              controller: 'DashboardController as vm'
            }
          },
          resolve: {
            authenticated: authenticated
          }
        })

app.run.js

function runBlock($rootScope, $log, $state) {
    $rootScope.$on('$stateChangeError', function () {
      // Redirect user to forbidden page
      $state.go('forbidden');
    });
  }

auth.factory.js

'使用严格的';

angular
  .module('app')
  .factory('authFactory', authFactory);

authFactory.$inject = ['$http', '$cookies'];

function authFactory($http, $cookies) {
  var _token;
  var service = {
    authenticate: authenticate
  };
  return service;

  // used to prevent user from accessing pages that they shouldn't have access to
  // this is used exclusively in app.routes.js/app.states.js
function authenticate() {
     // gets user's token from cookies, if no cookie, _token will be blank and server will return 403
    // this part might be redundant with other functions, but I left it in for now just to make sure
    if ($cookies.getObject('user')) {
      _token = $cookies.getObject('user').token;
    } else {
      _token = '';
    }
    var request = $http({
      method: 'POST',
      url: 'http://localhost:8080/checkToken',
      headers: {'x-auth-token': _token},
      transformResponse: function(data) {
        return data;
      }
    });
    return request;
  }
}

1 个答案:

答案 0 :(得分:2)

您需要将return deferred.promise置于函数外部,以便正确返回承诺。

<强>代码

function authenticated(authFactory, $q, $log) {
  var deferred = $q.defer();
  authFactory.authenticate()
  .then(function(authenticate) {
    if (authenticate.data === 'true') {
      deferred.resolve();
    } else {
      deferred.reject();
    }
  });
  return deferred.promise; //placed outside function
}