witoldsz / angular-http-auth

MIT License
2.38k stars 417 forks source link

Authorization header not sent after log out and new log in #67

Closed ghost closed 10 years ago

ghost commented 10 years ago

Hi,

Thanks for this great module.

My issue is the following: The first log in works perfectly. But then, if I log out and try to log in again, the http request that it is stored in the buffer is sent without the http authorization headers. If I continue browsing the site, the following http requests do carry the authorization token. So the problem it is just in the buffered request after a log out and a new log in.

This is my first angularjs app, so probably I'm doing something wrong, but I have been hours trying to fix this and I do not know what else I can try. I have followed the demo, and in my LoginController, I have something like this for the submit button of the login form:

$http.post('api/login', loginInfo).success(function(data) { $http.defaults.headers.common['Authorization'] = 'Basic ' + data.token; authService.loginConfirmed(); });

Am I using the module correctly? Any hint regarding why the first log in works like a charm but if I log out and then log in, the buffered request is sent without the token?

Thank you very much.

witoldsz commented 10 years ago

In your case, you have to update the headers of buffered requests (before my module sends them again). To do it, create a function and pass it as a second argument to #authService.loginConfirmed method. See the source code for reference and the "Advanced use case" paragraph of README.md.

ghost commented 10 years ago

And the answer was in the README... shame on me. Thanks a lot for your help.

In case some newbie like me has this problem in the future, the answer is something like:

$scope.request_updater = function(req) { req.headers.Authorization = 'Basic ' + data.token; return req; }

authService.loginConfirmed([], $scope.request_updater);

witoldsz commented 10 years ago

I am glad it worked for you :) BTW: there is no need to attach this function to $scope. There is also no need to pass an empty array as a first argument. null or undefined would do as well.