2011-12-17 56 views
4

我使用devise進行API認證。我已經覆蓋SessionController:create方法是這樣的:warden custom_failure

class Users::SessionsController < Devise::SessionsController 

    def create 
    resource = warden.authenticate!(:scope => resource_name, :recall => "#{controller_path}#new") 
    set_flash_message(:notice, :signed_in) if is_navigational_format? 
    val = sign_in(resource_name, resource) 
    current_user.reset_authentication_token! 

    respond_to do |format| 
     format.html do 
     respond_with resource, :location => redirect_location(resource_name, resource) 
     end 
     format.json do 
     render :json => { :status => OK_OK, :auth_token => current_user.authentication_token }.to_json, :status => :ok 
     end 
    end 
    end 
end 

正如你可以看到我用JSON進行身份驗證時,狀態碼響應。當驗證失敗時,我收到以下回復:

{"error":"Invalid email or password."} 

如何更改此消息?我不知道在哪裏覆蓋這個warden.authenticate!方法。

回答

1

您的語言環境文件(在配置/區域設置/目錄中的文件),這取決於你的國際化配置的(英語是en.yml)補充一點:

en: 
    devise: 
    invalid: 'My custom message for invalid login' 

當然,替代My custom message for invalid login的消息你想。

+2

不,這不是一個解決方案。正如你所看到的,我不想改變消息本身,但發送一些我想要的代碼。例如,它是:'render:json => {:status => OK_OK,:auth_token => current_user.authentication_token} .to_json,:status =>:ok'以進行正確的身份驗證。對於不正確的我想要的東西是:'render:json => {:status => ERROR_AUTH} .to_json,:status =>:unauthorized'。 – Mateusz 2011-12-18 23:31:20

相關問題