Skip to content
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

Open
wants to merge 6 commits into
base: master
Choose a base branch
from

Conversation

NikolajDL
Copy link

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.

Nikolaj Dam Larsen and others added 6 commits August 2, 2014 01:36
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.
@kitpymes
Copy link

Hello Taiseer,
how are you?
first of all thanks for sharing your knowledge, and excuse me for my English is very bad.
I'm trying to test your application but locally:

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!

@Leftyx
Copy link

Leftyx commented Sep 24, 2014

@ferrarisebastian It should work. I have just tested it and everything works fine.
Make sure you have compiled the whole solution and IIS Express has loaded all the applications.
Make sure that AngularJSAuthentication.API is using the port 26264 otherwise you have to change it accordingly in your script.

@kitpymes
Copy link

Hello leftyx,
thanks for your answer, but I've tried everything you say, both projects load correctly in their respective ports iisexpress,
API: http: // localhost: 26264 / Y WEB: http: // localhost: 32150 /.

In app.js:
ServiceBase var = 'http: // localhost: 26264 /';

Error browser:
Request from source other locked: same origin policy prevents reading the remote resource http: // localhost: 26264 / api / account / register. This can be fixed by moving the action to the same domain or activating CORS.

Error sign up register:
Failed to register user due to:

@vikram-katkar
Copy link

Hello Excolo, how can we exclude some request from interceptor. I wish to hit a request without OAuth token in header.
Thanks in advance.

@@ -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) {
Copy link

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?

Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants