-
Notifications
You must be signed in to change notification settings - Fork 758
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
Added support for auto refresh of tokens. #2
base: master
Are you sure you want to change the base?
Conversation
Removed the check for authData - this is already checked in 'authService.refreshToken()' so were doing double work here. Moved the authService.logOut() call into the refreshToken promise reject callback.
Hello Taiseer, var serviceBase = 'http://localhost:26264/'; It turns out that when I want to REGISTER ( http://localhost:26264/api/Account/Register ), shoot me 404 Not Found error, and have tried various things like enable cross domain you everywhere, and I keep going the same. You know how to solve this problem? Muchas gracias! |
@ferrarisebastian It should work. I have just tested it and everything works fine. |
Hello leftyx, In app.js: Error browser: Error sign up register: |
Hello Excolo, how can we exclude some request from interceptor. I wish to hit a request without OAuth token in header. |
@@ -1,7 +1,8 @@ | |||
'use strict'; | |||
app.factory('authService', ['$http', '$q', 'localStorageService', 'ngAuthSettings', function ($http, $q, localStorageService, ngAuthSettings) { | |||
app.factory('authService', ['$q', '$injector', 'localStorageService', 'ngAuthSettings', function ($q, $injector, localStorageService, ngAuthSettings) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do you prefer injecting $http using $injector instead of adding $http as a dependency?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is to overcome the issue of circular dependencies. When configuring the interceptor, you make the $httpProvider depend on the interceptor. So if you try to inject $http in the service constructor, you'll get a circular dependency error. To overcome this, I use the $injector service, to postpone injecting the $httpProvider until after everything is ready.
This is also the reason why $injector.get('authService'); is injected using $injector, since the authService depends on $http as well.
In the comment section of Enable OAuth Refresh Tokens in AngularJS... part 3, there was a wish to enable seamless refresh token requests.
One way of doing this is to intercept the http response error and use the ideas in the angular-http-auth library to refresh the token and retry the original http request.
This commit does the job. :-)
I've basically replaced the code that relocates to the refresh page in the response error interceptor, with a call to the refreshToken() method in the authService, and if this call is succesful, we retry the intercepted http request.
The $http service is being injected by the $injector service instead of through the service constructor arguments, in order to avoid getting the cyclic dependency error. This is done both in the authInterceptorService and the authService.