ng-token-auth not persisting



我目前使用ng-token-auth和Rails的devise-token-auth有一个非常简单的应用程序。它运行良好,$ auth方法运行良好(例如,登录)。但是,在页面上刷新access-token不会持续,也不会使用ipcookie写信给cookie。

我的Rails应用程序正在转发适当的标题:

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:tester53@mailinator.com
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

但是,当它通过ng-token-authupdateHeadersFromResponse方法运行时,标头返回null-特别是在ng-token-auth.js的第588行中

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);
  }
};

有人遇到过吗?为什么不会传递此$httpProvider方法?

我在这篇文章中找到了答案。

我正在使用多域设置,因此API域需要在CORS配置中具有一些额外的裸露标头。因此,在Rails-Cors Gem中,我为访问式添加了一个额外的裸露值:

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

更新:暴露了Angular所需的所有字段,而不仅仅是使其起作用的一个字段。

最新更新