1

这是我在我的应用程序中实现登录过程的代码。应用程序必须验证服务器设置的 cookie 并通过将用户重定向到画布状态来继续登录过程。我的问题是我收到上述错误。实际上它可以成功登录,但我想摆脱这个错误。我想错误应该在 $stateChangeStart 但我不知道如何解决它。任何想法?

(function() {
  'use strict';

  angular
    .module('app', [
      'ui.router',
      'ngResource',
      'ngCookies',
      'app.login'
    ])
    .config(function($stateProvider, $urlRouterProvider){
      $urlRouterProvider.otherwise('/login');
    })
    .run(function($rootScope, AuthService, RedirectService) {
      $rootScope.$on('$stateChangeStart', function(event, toState) {
        if (!AuthService.isAuthenticated()) {
          // the user isn't authenticated
          event.preventDefault();
          // redirect to the server side
          RedirectService.redirectToAuth();
        }
      });
    });
})();




(function() {
  'use strict';

  angular
    .module('app.login')
    .factory('AuthService', Auth);

  Auth.$inject = ['Cookie', 'Session'];

  function Auth(Cookie, Session) {

    return {
      login: function(params) {
        // here set the session with params passed by the server
        Session.create(params.id, params.data.id, params.data.make, params.data.name);
      },
      isAuthenticated: function() {
        // check cookie here set in the server side
        return Cookie.exist();
      }
    };

  }

})();


(function() {
  'use strict';

  angular
    .module('app.login')
    .service('Cookie', Cookie);

  Cookie.$inject = ['$cookies'];

  function Cookie($cookies) {

    this.authCookie = $cookies.__cookie;

    this.exist = function() {
      return (this.authCookie ? true : false);
    };
  }
})();

(function() {
  'use strict';

  angular
    .module('app.login')
    .factory('RedirectService', Redirect);

  Redirect.$inject = ['$window'];

  function Redirect($window) {

    return {
      redirectToAuth: function() {
        // redirect the user to the server for auth
        $window.location.href = "http://" + $window.location.host + "/auth/facebook";
      }
    };
  }
})();


(function() {
  'use strict';

  angular
    .module('app.login')
    .controller('LoginController', Login);

  // here inject what function Login needs
  Login.$inject = ['$rootScope', '$scope', '$state', '$stateParams', 'AuthService'];

  function Login($rootScope, $scope, $state, $stateParams, AuthService) {

    var params = {
      id: $stateParams.userid,
      data: {
        id: $stateParams.modelid,
        make: $stateParams.modelmake,
        name: $stateParams.modelname
      }
    };

    $scope.login = function(params) {
      AuthService.login(params);
      // activate the canvas state
      $state.go('canvas');
    };

    // run the login function to set the Session user with data passed by the server
    $scope.login(params);
  }
})();
4

2 回答 2

1

也许这可以帮助更多:

/*
We are using the below urlRouterProvider.otherwise() because of:
https://github.com/angular-ui/ui-router/issues/600
 */
$urlRouterProvider.otherwise(function($injector, $location) {
    var $state = $injector.get('$state');
    $state.go('login');
});

使用这个代码你仍然可以使用otherwise(),使用的缺点when()是其他未知路由将无法匹配。上面的代码解决了我们所有的无限循环。

于 2015-04-22T07:54:21.567 回答
0

修复了问题

  $urlRouterProvider.otherwise('/login');

导致了这一切问题。

将其删除并替换为 when() 解决了问题

于 2015-04-21T22:06:25.303 回答