title: AngularJS not intercepting direct requests from Address Bar
body: <p>I'm creating an AngularJS application that uses the JWT token for authentication. The token is being passed using the AngularJS interceptor as shown below.</p>
  
  <pre><code>'request': function(config)
          {
              if (store.get('jwt_token') != null)
              {
                  config.headers['x-access-token'] = store.get('jwt_token');
              }
              else
              {
                  config.headers['x-access-token'] = '';
              }
  
              return config;
          }
  </code></pre>
  
  <p>Whenever I'm accessing any <strong><code>/restricted</code></strong> pages, everything is working fine. The issue is when I'm going to the <strong><code>/restricted</code></strong> page by directly typing the URL in the address bar (or refreshing the page), the AngularJS gets circumvented, and hence, the Interceptors don't intercept the request, and the token is not passed.</p>
  
  <p>I've been searching for a while, I found some solutions like responding with a piece of code that loads the AngularJS then makes a redirect from there. However, <strong>I'm looking for a simpler/neater approach if possible as I might be missing something here</strong>.</p>
  
  <p>I can detect if the request came from AngularJS or not by checking for the <strong><code>x-access-token</code></strong> since I'm always passing it (empty value if user is not authenticated).</p>
  
  <p><strong>Solution</strong></p>
  
  <p>Alex's answer is pointing to the exact problem, thanks Alex.</p>
  
  <p>I finally figured it out yesterday. The solution I went with was to make sure all the requests come from AngularJS, I have a list of the restricted pages, if any of them is requested, I'm calling a function to verify and validate the JWT token on server side, if it's valid, proceed, otherwise, go to login page. <strong>The key thing is to ensure that ALL requests should go to the index.html to make sure AngularJS is handling the routing.</strong></p>
  
  <p>This link helped me greatly to solve this issue.</p>
  
  <p><a href="http://arthur.gonigberg.com/2013/06/29/angularjs-role-based-auth/" rel="nofollow">http://arthur.gonigberg.com/2013/06/29/angularjs-role-based-auth/</a></p>