5
我设法设置了json身份验证。我执行下面的代码:通过json设计失败身份验证返回html而不是json
class Users:: SessionsController < Devise::SessionsController
def create
respond_to do |format|
format.html { super }
format.json {
warden.authenticate!(:scope => resource_name, :recall => "#{controller_path}#failure")
render :json => {:success => true}
}
end
end
def destroy
respond_to do |format|
format.html {super}
format.json {
Devise.sign_out_all_scopes ? sign_out : sign_out(resource_name)
render :json => {}
}
end
end
def failure
render :json => {:success => false, :errors => ["Login Failed"]}
end
end
这工作得很好,但在验证失败,失败犯规返回JSON失败。对于设计我有一个自定义的失败。如果我删除了redirect_url或完全删除了客户失败,那么身份验证将返回带有失败消息的json。我的自定义故障如下:
class CustomFailure < Devise::FailureApp
def redirect_url
#return super unless [:worker, :employer, :user].include?(scope) #make it specific to a scope
'/'
end
# You need to override respond to eliminate recall
def respond
if http_auth?
http_auth
else
redirect
end
end
末
反正保持重定向如果一个HTML请求,并具有故障味精如果JSON请求返回一个JSON?
谢谢!