witoldsz / angular-http-auth

MIT License
2.38k stars 417 forks source link

Broadcast event:auth-loginRequired earlier #61

Closed derek-pavao closed 10 years ago

derek-pavao commented 10 years ago

The API I am working with returns a 401 status for failed login attempts. This is causing a problem when the user gets prompted to login, types an invalid username and password, the failed login attempt gets pushed onto the httpBuffer to be retried later. Then when the user enters a valid username and password the invlalid attempt is retried, which produces a 401 from the api pushing them back to the login screen.

My solution is to just broadcast the 'event:auth-loginRequired' event before checking if rejection.config.ingoreAuthModule is set to true. This will give the developer a chance to set rejection.config.ignoreAuthModule to true in their event handler should they so choose to not have the failed request pushed on to the httpBuffer.

witoldsz commented 10 years ago

I don't get it - why is your 401 response pushed into buffer? As you can see here:

if (rejection.status === 401 && !rejection.config.ignoreAuthModule) {
  var deferred = $q.defer();
  httpBuffer.append(rejection.config, deferred);
  $rootScope.$broadcast('event:auth-loginRequired', rejection);
  return deferred.promise;
}

, your request is not going to be buffered (and retried later) if you configure ignoreAuthModule=true.

derek-pavao commented 10 years ago

I guess maybe I just dont understand where I'm supposed to set ignoreAuthModule from?

witoldsz commented 10 years ago

Set it as a parameter of the $http request. Once the response arrives, the interceptor can inspect the parameter (as in the snippet I have shown) and it won't act upon it.

witoldsz commented 10 years ago

Does it work for you now, with ignoreAuthModule=true added to config of login request?

witoldsz commented 10 years ago

Old, inactive, closing. Please reopen if needed.