开发者

custom sign in for devise

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: {"aut开发者_运维技巧h_result":"1"} instead of redirect?

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


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!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜