2011-03-01 74 views
3

現在我使用的是devise gem,成功授權後它將用戶重定向到根頁面。定製登錄設計

如何修改sign_in動作返回json數組像這樣:{"auth_result":"1"}而不是重定向?

可能嗎? 我還沒有在設計wiki中找到這些食譜。

回答

2

設計目前不處理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" }

希望這有助於!

+0

感謝您的回答!我已成功切換到自己的授權碼,我感到高興;) – Kir 2011-03-05 15:37:01