title: AngularJS: Protecting routes with angularjs depending if the user is authorized?
body: <p>I have just started out working with an <code>AngularJS</code> app I'm developing, everything is going well but I need a way of protecting routes so that a user wouldn't be allowed to go to that route if not logged in. I understand the importance of protecting on the service side also and I will be taking care of this.</p>
  
  <p>I have found a number of ways of protecting the client, one seems to use the following</p>
  
  <pre><code>$scope.$watch(
      function() {
          return $location.path();
      },
      function(newValue, oldValue) {
          if ($scope.loggedIn == false &amp;&amp; newValue != '/login') {
              $location.path('/login');
          }
      }
  );
  </code></pre>
  
  <p>where do I need to put this, in the <code>.run</code> in the <code>app.js</code>?</p>
  
  <p>And the other way I have found is using a directive and using an on - routechagestart</p>
  
  <p>the info is here  <a href="http://blog.brunoscopelliti.com/deal-with-users-authentication-in-an-angularjs-web-app">http://blog.brunoscopelliti.com/deal-with-users-authentication-in-an-angularjs-web-app</a></p>
  
  <p>I would really be interested in anyones help and feedback on the recommended way.</p>