custom sign in for devise

倖福魔咒の 提交于 2020-01-04 02:46:09

问题


Now I am using the devise gem, and after successful authorization it redirects user to the root page.

How to modify sign_in action to return json array like this: {"auth_result":"1"} instead of redirect?

Is it possible? I haven't found recipes for these in devise wiki.


回答1:


Devise currently does not handle JSON responses - see the issue on github. Therefore you would have to create a custom sessions_controller to handle your response. Take a look at the implementation on github to get an idea. I threw together some rough pseudo code that will hopefully get you started:

class Users::SessionsController < Devise::SessionsController
  def create
    resource = warden.authenticate!(:scope => resource_name, :recall => "#{controller_path}#new")
    set_flash_message :notice, :signed_in
    # Remove sign_in_and_redirect(resource_name, resource) and replace with the following, I think
    sign_in(resource_name, resource)
    respond_to do |format|
      format.json { render :json => hash_of_things_to_return.to_json }
    end
  end
end

Then in your routes.rb file you will have to specify your new controller - devise_for :users, :controllers => { :sessions => "users/sessions" }

Hope this helps!



来源:https://stackoverflow.com/questions/5159237/custom-sign-in-for-devise

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!