如何使用rails devise和iOS发送会话cookie

时间:2014-04-11 08:02:18

标签: ruby-on-rails cookies curl devise

我尝试使用

在服务器中进行授权
curl -v -H "Accept: application/json" -H "Content-type: application/json" -X POST -d ' {"user":   {"email":"EgorkZe@gmail.com","password":"1234"}}'  http://127.0.0.1:3000/users/sign_in

它给我回复

session = 654a8f9be39839e0eac5b06c7224d671;
success = 1;
user =     {
    "authentication_token" = "<null>";
    "created_at" = "2014-04-10T08:54:28.813Z";
    email = "egorkze@gmail.com";
    id = 1;
    "server_id" = "<null>";
    "updated_at" = "2014-04-11T07:27:23.580Z";
};

这是ruby sessions_controller.rb

    class SessionsController < Devise::SessionsController
    skip_before_filter :verify_authenticity_token, only: [:show, :create, :edit, :update, :destroy]

  def create

    resource = warden.authenticate!(:scope => resource_name, :recall => "sessions#failure")
    sign_in(resource_name, resource)

    respond_to do | format |
      format.json { render :json => { :success => true, :user => resource ,:session => session['session_id']}, :status => 200 }
    end

  end

  def failure
    respond_to do | format |
      format.json { render :status => 401, :json => {:success => false, :errors => ["Login failed."]} }
    end

  end  
end

如何发送会话cookie以及如何保持会话。当我发送到带有curl的不同获取请求的rails时,它会给我“”登录失败。“”

1 个答案:

答案 0 :(得分:0)

默认情况下,cUrl不保存cookie。您可以添加--cookie-jar <filename>以将cookie保存到磁盘上的文件中。您也可以手动传递您拥有cookie的会话ID:

-b "session=654a8f9be39839e0eac5b06c7224d671"

我不确定这与iOS有什么关系? nsHTTP应该正确处理Cookie(尽管它与protect_from_forgery不匹配。