I have a pretty bare-bones application at the moment using ng-token-auth
and rails' devise-token-auth
. It is running fine and the $auth methods are working well (for example, logging in). However, upon page refresh the access-token
doesn't persist and doesn't get written to the cookie using ipCookie.
My rails app is forwarding the proper headers as seen below:
Access-Control-Allow-Credentials:true
Access-Control-Allow-Methods:GET, POST, DELETE, PUT, OPTIONS, HEAD
Access-Control-Allow-Origin:http://localhost
Access-Control-Expose-Headers:
Access-Control-Max-Age:0
access-token:XXXXXXXXXXXXXX
Cache-Control:max-age=0, private, must-revalidate
client:nYMXLxnuO7BIGZkdXkZ_Xg
Connection:Keep-Alive
Content-Type:application/json; charset=utf-8
Date:Sat, 09 May 2015 21:41:56 GMT
ETag:"c16291f5079691a2528d5a7876627ede"
expiry:1431294116
Keep-Alive:timeout=5, max=99
Server:Apache/2.4.10 (Unix) PHP/5.5.20
token-type:Bearer
Transfer-Encoding:chunked
uid:[email protected]
Vary:Origin
X-Content-Type-Options:nosniff
X-Frame-Options:SAMEORIGIN
X-Request-Id:2b224904-944c-48f7-aa4e-d0407e26e893
X-Runtime:0.230242
X-XSS-Protection:1; mode=block
But when it gets run through the updateHeadersFromResponse
method of ng-token-auth
the headers are returning null-- specifically in lines 588 of ng-token-auth.js
updateHeadersFromResponse = function($auth, resp) {
var key, newHeaders, val, _ref;
newHeaders = {};
_ref = $auth.getConfig().tokenFormat;
for (key in _ref) {
val = _ref[key];
if (resp.headers(key)) {
newHeaders[key] = resp.headers(key);
}
}
if (tokenIsCurrent($auth, newHeaders)) {
return $auth.setAuthHeaders(newHeaders);
}
};
Has anyone encountered this? Why would the header not be passed to this $httpProvider
method?
I found the answer in this post Angular.js saying custom HTTP response header is null
I was using a multi-domain setup so the API domain needed to have some additional exposed headers in the CORS configuration. So in the rails-cors gem I added an additional exposed value for the access-token:
config.middleware.insert_before 0, "Rack::Cors" do
allow do
origins 'api.com'
resource '*',
:headers => :any,
:methods => [:get, :post, :delete, :put, :options, :head],
:expose => ['access-token', 'expiry', 'token-type', 'uid', 'client'],
:max_age => 0
end
end
UPDATE: Exposed all fields required by angular, not just one ones that made it work.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With