【问题标题】:Devise and Authentication with CURL !使用 CURL 设计和验证!
【发布时间】:2011-09-18 00:08:15
【问题描述】:

我希望能够通过 curl 样式进行身份验证!

所以我想在登录时获得一个令牌:

curl -X POST 'http://localhost:3000/users/sign_in.json' -d 'user[email]=em...@provider.us&user[password]=pass' 

但如果我执行此操作,我只会得到:

{"email":"em...@provider.us","name":"Name of the user"} 

如何添加一些特定字段,例如 authentication_token ?

我想做的事对吗?还有其他更好的方法吗 这 ?

谢谢!

【问题讨论】:

标签: ruby-on-rails-3 curl devise


【解决方案1】:

我创建了一个名为 SessionController 的控制器

class Users::SessionsController < Devise::SessionsController
  append_view_path 'app/views/devise'
    def create
      respond_to do |format|
        format.html { super }
        format.json {
          warden.authenticate!(:scope => resource_name, :recall => "#{controller_path}#new")
          current_user.ensure_authentication_token!
          render :json => {:user => current_user.api_attributes, :auth_token => current_user.authentication_token}.to_json, :status => :ok
        }
      end
    end

    def destroy
      respond_to do |format|
        format.html { super }
        format.json {
          warden.authenticate!(:scope => resource_name, :recall => "#{controller_path}#new")
          current_user.empty_authentication_token!
          render :json => {}.to_json, :status => :ok
        }
      end
    end
end

并将其添加到根文件中:

devise_for :users, :controllers => { :sessions => "users/sessions" }

它有效:-)

【讨论】:

  • 我试过了,但我仍然无法使用 Curl 注销。它只是将我重定向到 sign_in.json。您用来退出的实际 Curl 命令是什么。这没有用:stackoverflow.com/questions/7997009/…@Arkan
猜你喜欢
  • 2015-05-04
  • 2018-11-29
  • 1970-01-01
  • 2013-06-20
  • 2013-10-05
  • 1970-01-01
  • 1970-01-01
  • 2015-03-10
  • 1970-01-01
相关资源
最近更新 更多