我使用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!
方法。
不,這不是一個解決方案。正如你所看到的,我不想改變消息本身,但發送一些我想要的代碼。例如,它是:'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