I am using MEAN Stack, and jade for html templating for developing a 
project. Since I am using jade all the .when() of the $routeProvider goes 
through the server and then rendered. Things were absolutely fine until 
recently I added $httpProvider.interceptor for authenticating each request. 
Basically, I started using JWT token based authentication and before 
requesting anything to the server, in my interceptor I am attaching 
'Authorization' header. And the first middleware for protected apis I am 
verifying the token present in the 'Authorization' header. If present then 
proceed, else send authorization error.

Now, as mentioned in the title of the question. I have a few URLs 
containing hypen(-) between two words just to increase the readability.

eg: .when('/firstPart/secondPart1-secondPart2/thrirdPart', {
         templateUrl: '/path/to/file'
     })

this url is working fine if user is going through the normal flow of 
getting to this url. The problem arises when the user comes to this page 
and hits the browser's refresh button, as he gets an error 'Authorization 
header not set'

I did a few research on what and why this is happening, I found the answer 
in AngularJS not intercepting direct requests from Address Bar 
<http://stackoverflow.com/questions/30988271/angularjs-not-intercepting-direct-requests-from-address-bar>.
 
According, to this answer and few others I understood that on refreshing, 
browser makes a direct request to the server and doesn't go through angular 
routes, and thus, interceptor that is attaching the Authorization header to 
each request header is not getting called.

As, suggested by the same link, the workaround is that if auth verification 
fails make a call to '/', I donot wish to do that as the user has just 
refreshed the page and he has to ideally get back the same page as he was 
before he pressed refreshed. I am stuck here on how to do this.

Also, there is another mind–boggling thing i.e., if, I remove the 
'hyphen'(-), and make it one word i.e.,

.when('/firstPart/secondPart1SecondPart2/thrirdPart',
 {
      templateUrl: '/path/to/file'
  })

everything works as expected even when the user hits the refresh button. It 
is first going through the angular router and interceptor attaches the 
Authorization header and then goes to make a *get* request for that 
template.

Wondering, is there any problem in using hyphen in URLs? However, I do not 
wish to remove the hyphens as I have already used it in many places and 
also I feel its good to have - between two words that needs to be displayed 
together in the URL than making them camel cased.

So, finally I have two questions :

   1. How to load the same page as the user was in before he hit refresh, 
   instead of redirecting him to '/' or login url.
   2. The problem explained above, occurs only for hyphenated(-) URLs, what 
   is the problem having '-' in the URL.

-- 
You received this message because you are subscribed to the Google Groups 
"Angular" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.

Reply via email to