title: Angular ui-router user authentication check
body: <p>I'm trying to create a very simple authentication control using ui-router. I do the checking in the backend, though, I want to redirect users if they aren't logged in.</p>
  
  <p>However, when I try to visit /profile, nothing happens. If I console.log() a text like "yes", it's being logged out unlimited amount of time until I get stack size exceeded error. It keeps hitting /authcheck url, I can see that via network tools of Chrome.</p>
  
  <p>If I remove <code>$state.go(toState.name)</code>, I see a blank page and <code>console.log()</code> shows that user is signed in (which I am), but then I can't see <code>/profile</code> page, as expected.</p>
  
  <p>What could be the problem here?</p>
  
  <p><code>/authcheck</code> returns <code>200</code> or <code>403</code>.</p>
  
  <p>my factory</p>
  
  <pre><code>angular.module('site.profile').factory('Profile', ["$http", function($http) {
    return {
      authCheck: function() {
        return $http.get('/authcheck');
      }
    };
  }]);
  </code></pre>
  
  <p>ui-router</p>
  
  <pre><code>angular.module('site.profile.routes')
  .run(
    ['$rootScope', 'Profile', '$state', '$stateParams',
      function($rootScope, Profile, $state, $stateParams) {
        $rootScope.$state = $state;
        $rootScope.$stateParams = $stateParams;
        $rootScope.$on('$stateChangeStart', function(event, toState, toParams, fromState, fromParams) {
          if (toState.name !== 'auth') {
            event.preventDefault();
            Profile.authCheck().then(function(returned) {
              if (returned.status === 200) {
                console.log("yes!");
                console.log(toState.name);
                //$state.go($state);
              } else {
                $state.go('auth');
              }
            });
          }
        });
      }
    ]
  )
  
  
  .config(
    ['$stateProvider', '$urlRouterProvider', '$locationProvider',
      function($stateProvider, $urlRouterProvider, $locationProvider) {
  
        $locationProvider.html5Mode({
          enabled: true,
          requireBase: false
        });
  
        $stateProvider
          .state('profile', {
            url: '/profile',
            views: {
              '': {
                templateUrl: 'views/profile/profile.html'
              },
              'post@profile': {
                templateUrl: 'views/post.html'
              },
              'nav@profile': {
                templateUrl: 'views/shared/nav/nav.html'
              },
              'top@profile': {
                templateUrl: 'views/profile/top.html'
              }
            }
          })
          .state('auth', {
            url: '/signin',
            template: "please sign in"
          });
      }
    ]);
  </code></pre>