ruby-on-rails – 通过json设计失败认证发回html而不是json
发布时间:2020-12-17 03:14:35 所属栏目:百科 来源:网络整理
导读:我设法设置了json身份验证.我实现了以下代码: class Users:: SessionsController Devise::SessionsController def create respond_to do |format| format.html { super } format.json { warden.authenticate!(:scope = resource_name,:recall = "#{controll
我设法设置了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请求? 谢谢! 解决方法
你必须告诉warden使用那个自定义失败.
def failure respond_to do |format| format.html {super} format.json do warden.custom_failure! render :json => {:success => false,:errors => ["Login Failed"]} end end end (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |