如何使用Devise签署用户跨域

时间:2013-11-20 12:56:54

标签: ruby-on-rails angularjs devise cross-domain cors

我正在使用Devise注册,登录,从AngularJS客户端注销用户,注册和登录工作正常,但退出似乎对我失败,我不知道问题出在哪里。

运行以下代码客户端时:

$http({
    method : 'DELETE',
    url : 'http://localhost:3000/users/sign_out'
}).success(function(response) {
   console.log(response);
}).error(function(response) {
   console.log(response);
});

我的浏览器控制台说明如下:

OPTIONS http://localhost:3000/users/sign_out 404 (Not Found) angular.min.js:72
OPTIONS http://localhost:3000/users/sign_out No 'Access-Control-Allow-Origin' header is      present on the requested resource. Origin 'http://karm.local.mobi' is therefore not allowed access. angular.min.js:72
XMLHttpRequest cannot load http://localhost:3000/users/sign_out. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://karm.local.mobi' is therefore not allowed access. (index):1

终端中的webbrick说:

Started OPTIONS "/users/sign_out" for 127.0.0.1 at 2013-11-20 13:45:08 +0100
ActionController::RoutingError (No route matches [OPTIONS] "/users/sign_out"):
actionpack (3.2.13) lib/action_dispatch/middleware/debug_exceptions.rb:21:in `call'
actionpack (3.2.13) lib/action_dispatch/middleware/show_exceptions.rb:56:in `call'
railties (3.2.13) lib/rails/rack/logger.rb:32:in `call_app'
railties (3.2.13) lib/rails/rack/logger.rb:16:in `block in call'
activesupport (3.2.13) lib/active_support/tagged_logging.rb:22:in `tagged'
railties (3.2.13) lib/rails/rack/logger.rb:16:in `call'
actionpack (3.2.13) lib/action_dispatch/middleware/request_id.rb:22:in `call'
rack (1.4.5) lib/rack/methodoverride.rb:21:in `call'
rack (1.4.5) lib/rack/runtime.rb:17:in `call'
activesupport (3.2.13) lib/active_support/cache/strategy/local_cache.rb:72:in `call'
rack (1.4.5) lib/rack/lock.rb:15:in `call'
actionpack (3.2.13) lib/action_dispatch/middleware/static.rb:63:in `call'
railties (3.2.13) lib/rails/engine.rb:479:in `call'
railties (3.2.13) lib/rails/application.rb:223:in `call'
rack (1.4.5) lib/rack/content_length.rb:14:in `call'
railties (3.2.13) lib/rails/rack/log_tailer.rb:17:in `call'
rack (1.4.5) lib/rack/handler/webrick.rb:59:in `service'
/Users/sam/.rvm/rubies/ruby-1.9.3-p392/lib/ruby/1.9.1/webrick/httpserver.rb:138:in `service'
/Users/sam/.rvm/rubies/ruby-1.9.3-p392/lib/ruby/1.9.1/webrick/httpserver.rb:94:in `run'
/Users/sam/.rvm/rubies/ruby-1.9.3-p392/lib/ruby/1.9.1/webrick/server.rb:191:in `block in start_thread'


Rendered /Users/sam/.rvm/gems/ruby-1.9.3-p392/gems/actionpack-3.2.13/lib/action_dispatch/middleware/templates/rescues/routing_error.erb within rescues/layout (0.4ms)

然而,当我使用像Dev HTTP Client之类的东西时,webbrick终端似乎工作得很好:

Started DELETE "/users/sign_out" for 127.0.0.1 at 2013-11-20 13:51:45 +0100
Processing by SessionsController#destroy as */*
Completed 200 OK in 0ms (Views: 0.1ms | ActiveRecord: 0.0ms)

这对我来说意味着跨域是问题所在。但是我登录或注册用户的调用与DELETE相同。允许CORS的ruby双边过滤器对于所有Devise方法也是相同的。

我错过了一些明显的东西吗?或者还有另一种方法可以解决这个问题吗?提前谢谢。

在我的控制器中允许CORS的方法:

def cors_set_access_control_headers
  headers['Access-Control-Allow-Origin'] = '*'
  headers['Access-Control-Allow-Methods'] = 'POST, GET, OPTIONS, DELETE, PUT'
  headers['Access-Control-Max-Age'] = "1728000"
end

def cors_preflight_check
  if request.method == :options
    headers['Access-Control-Allow-Origin'] = '*'
    headers['Access-Control-Allow-Methods'] = 'POST, GET, OPTIONS, DELETE, PUT'
    headers['Access-Control-Allow-Headers'] = 'X-Requested-With, X-Prototype-Version'
    headers['Access-Control-Max-Age'] = '1728000'
    render :text => '', :content_type => 'text/plain'
  end
end

更新

最终我能够使用Rack Cors来解决问题。仍然不确定什么是错的,但我很高兴它的排序。

0 个答案:

没有答案