title: Angular ui-router: how to prevent access to a state
body: <p>Hello I'm new to angularJS and have been trying to prevent access to certain states based on user critera.</p>
  
  <p><a href="https://github.com/angular-ui/ui-router/wiki/Frequently-Asked-Questions#how-to-create-rules-to-prevent-access-to-a-state" rel="noreferrer">This, from ui-router's FAQ</a> describes exactly what I want to do, but I cant get it to work properly. What do I need to but in the data object exactly to accomplish this?</p>
  
  <p>(I saw someone throwing in "true" on some blog post tutorial and using it like how I have, but that doesnt seem to work because I get an error that says needAdmin is not defined)</p>
  
  <p>Here is my code:</p>
  
  <pre><code>angular.module('courses').config(['$stateProvider',
      function($stateProvider) {
          // Courses state routing
          $stateProvider.
          state('listCourses', {
              url: '/courses',
              templateUrl: 'modules/courses/views/list-courses.client.view.html'
          }).
          state('createCourse', {
              url: '/courses/create',
              templateUrl: 'modules/courses/views/create-course.client.view.html',
              data: {
                  needAdmin: true
              }
          }).
          state('viewCourse', {
              url: '/courses/:courseId',
              templateUrl: 'modules/courses/views/view-course.client.view.html'
          }).
          state('editCourse', {
              url: '/courses/:courseId/edit',
              templateUrl: 'modules/courses/views/edit-course.client.view.html',
              data: {
                  needAdmin: true
              }
          });     
  
      }
  ]);
  
  
  angular.module('courses').run(['$rootScope', '$state', 'Authentication', function($rootScope, $state, Authentication) {
    $rootScope.$on('$stateChangeStart', function(e, to) {
  
      var auth = Authentication;
  
      console.log(auth.user.roles[0]);
      if (to.data.needAdmin &amp;&amp; auth.user.roles[0] !== 'admin') {
        e.preventDefault();
        $state.go('/courses');
      }
  
    });
  }]);
  </code></pre>