扩展设计SessionsController使用JSON验证 [英] Extending Devise SessionsController to authenticate using JSON
问题描述
我想建立一个iPhone应用程序导轨API。设计工作正常通过Web界面登录,但我需要能够创建和使用REST API破坏会议,我想使用,而不必做会话控制器上的POST和不必解析HTML和处理一个JSON重定向。
我想我可以做这样的事情:
类API :: V1 :: SessionsController<设计:: SessionsController
打造高清
超
结束
DEF破坏
超
结束
结束
和在配置/ routes.rb中我加:
命名空间:API做
命名空间:v1的做
资源:会话:只=> [:创建:毁灭]
结束
结束
耙路线显示路线是正确安装:
api_v1_sessions POST /api/v1/sessions(.:format){:行动=>中创造,:控制器=>中的API / V1 /会话}
api_v1_session DELETE /api/v1/sessions/:id(.:format){:行动=>中消灭,:控制器=>中的API / V1 /会话}
当我POST到/用户/会话一切工作正常。我得到一些HTML和302。
现在,如果我POST到/ API / V1 /会话我得到:
未知动作
在AbstractController :: ActionNotFound
块引用>卷曲-v -H内容类型:应用程序/ JSON'-H接受:应用/ JSON-X POST的http://本地主机:3000 / API / V1 /会议-d{用户:{'登录':'测试','密码':'foobar的'}}
解决方案这是最后的工作。
类API :: V1 :: SessionsController<设计:: SessionsController
打造高清
做的respond_to |格式|
format.html {}超
format.json {
warden.authenticate!(:范围=> RESOURCE_NAME,:召回=>中#{} controller路径#NEW)
渲染:状态=> 200:JSON => {:错误=> 成功}
}
结束
结束
DEF破坏
超
结束
结束同样改变routes.rb中,记住顺序很重要。
devise_for:用户:控制器=> {:会话=> API / V1 /会话}
devise_scope:用户做
命名空间:API做
命名空间:v1的做
资源:会话:只=> [:创建:毁灭]
结束
结束
结束资源:用户I am trying to build a rails API for an iphone app. Devise works fine for logins through the web interface but I need to be able to create and destroy sessions using REST API and I want to use JSON instead of having to do a POST on the sessions controller and having to parse the HTML and deal with a redirect.
I thought I could do something like this:
class Api::V1::SessionsController < Devise::SessionsController def create super end def destroy super end end
and in config/routes.rb I added:
namespace :api do namespace :v1 do resources :sessions, :only => [:create, :destroy] end end
rake routes shows the routes are setup properly:
api_v1_sessions POST /api/v1/sessions(.:format) {:action=>"create", :controller=>"api/v1/sessions"} api_v1_session DELETE /api/v1/sessions/:id(.:format) {:action=>"destroy", :controller=>"api/v1/sessions"}
When I POST to /user/sessions everything works fine. I get some HTML and a 302.
Now if I POST to /api/v1/sessions I get:
Unknown action AbstractController::ActionNotFound
curl -v -H 'Content-Type: application/json' -H 'Accept: application/json' -X POST http://localhost:3000/api/v1/sessions -d "{'user' : { 'login' : 'test', 'password' : 'foobar'}}"
解决方案This is what finally worked.
class Api::V1::SessionsController < Devise::SessionsController def create respond_to do |format| format.html { super } format.json { warden.authenticate!(:scope => resource_name, :recall => "#{controller_path}#new") render :status => 200, :json => { :error => "Success" } } end end def destroy super end end
Also change routes.rb, remember the order is important.
devise_for :users, :controllers => { :sessions => "api/v1/sessions" } devise_scope :user do namespace :api do namespace :v1 do resources :sessions, :only => [:create, :destroy] end end end resources :users
这篇关于扩展设计SessionsController使用JSON验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!