【问题标题】: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" }
它有效:-)