自定义登录设计

时间:2011-03-01 19:19:20

标签: ruby-on-rails-3 devise

现在我正在使用devise gem,并且在成功授权后,它会将用户重定向到根页面。

如何修改sign_in操作以返回这样的json数组:{"auth_result":"1"}而不是重定向?

有可能吗? 我还没有在设计维基中找到这些食谱。

1 个答案:

答案 0 :(得分:2)

Devise目前不处理JSON响应 - 请参阅issue on github。因此,您必须创建自定义sessions_controller来处理您的响应。看看implementation on github以获得一个想法。我把一些粗略的伪代码汇总在一起,希望能让你开始:

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

然后在routes.rb文件中,您必须指定新的控制器 - devise_for :users, :controllers => { :sessions => "users/sessions" }

希望这有帮助!