-
Notifications
You must be signed in to change notification settings - Fork 414
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to handle multiple 401 request #95
Comments
Hi @reggieboy, I had the same issue, the best solution I found was first do just one request which I was sure would get a 401 and consecuently would fire the loginRequired event. This can be a dummy request just to be sure the next ones are sent with a valid token. Cheers. |
@rolandocc is right, that should solve your problem. Try to keep the bootstrap process of your application tight: send a probe to figure the response code and wait until it's all OK before firing up controllers. |
I'm trying to do as @rolandocc suggests as well. I would like to put something in the route handler to prevent the application from changing location before probing for 401 on certain routes, or something similar. In such case I could preemptively stop the application from going there and having it return when login is canceled. Since right now I end up with partial page render in case of login cancel. |
Hi @rolandocc, @witoldsz and @wcyrek-comrise I love the idea of bootstrapping the process with one request first before firing the controller and going to a route. Unfortunately, I am not entirely sure on how to do it. This is what I have attempted so far and I am not sure if this is what you guys meant: In the router
Is this what you guys meant? I am not entirely happy with my method, since I am wondering if its better to perhaps have this under |
Hello there, What is your specific use case you cannot follow the simple solution from the project's README documentation? |
Hi @witoldsz I have already followed the 'simple' solution in README which resolves 90% of my queries with no problem. However, there is 1 use case that the http-interceptor doesnt resolve directly for me. In my app, I have protected images where a token needs to be sent with the image url parameter for the server to check the authentication and permissions before sending its response. If I use the Hope this makes sense :) |
Hi, thanks for explanation of your case, but still I cannot get the problem at it's root. As far as I understand, at some point your application has to get some kind of token, so the images can be displayed. What does it have to deal with router and routes? Let's just assume few things, so I can try to replay what is happening: User navigates your application and at some point they summon the part, where a controller needs to fetch the token, so it can provide some directive with URLs. Now, looking from the directive point of view we have two options: Option (a) is simple thanks to "promises". It asks for URLs, so controller just asks for token and return the promise. Using the "angular-http-auth" can help here, because if the process of fetching the token goes the 401 detour, the original promise returned to directive won't notice, assuming you won't spoil the process with some redirections or you won't destroy the current state in any other way. Option (b) is also simple, you have a token and your directive is behind the "ng-if" until the controller says it's ready. Question reminder: what does it all has to do with router? Why won't you leave it to "the" controller, or if you like, the directive with the logic built-in? |
First of all, I humbly thank you for taking the time to write back to me. Much obliged! There are 2 cases I have and I will explain a bit more on exact user navigation and process scenario I have so far: Scenario 1: Image Urls
Scenario 2: Image Upload This is a little similar to the above but when using the Nergh's Angular-File-Upload module.
After your suggestion, I did gave more serious thought on your advise and it did make sense to keep everything in controller itself rather than poking the router. This is what I have come up with for now: Attempted Solutions: Scenario 1 Solution: Doing a 2 step solution for this: (i) First added a dummy ping to the page controllers which doesnt have any server queries so a server auth-check is triggered. If 401 occurs, http-auth-interceptor will notice it.
(ii) Next in the login controllers, after the login is successful it calls a service which refreshes the
This method works fine, but the only drawback is I would need to remember to add other image urls to this service list in the future when I have to add images with similar scenario so all of them are refreshed. Scenario 2 Solution: Instead of using the module's default
I am not sure if the above is the right approach or if there is a better way to do it. But it certainly does avoid doing anything from the router like you had advised. Do let me know if you feel it can be done differently. Thanks again for taking the time to write to me. |
updated |
I have the library updated, but I still have the same problem, my token is generated validated, but the 401 are still infinite, someone has some solution I've already seen topics 95, 120, 130, 131 and nothing solved |
Hello! First of all good job on the interceptor :)
I have a scenario wherein i have multiple 401 response from my server
and each 401 response is intercepted by angular-http-auth then the event:auth-loginRequired is
triggered.
This will result into multiple request for a new token invalidating the previous ones.
Do you have a sample or can give some advice on how to handle this kind of scenario?
Cheers!
The text was updated successfully, but these errors were encountered: