[英]Devise won't call custom failure on session controller
我有以下自定義會話控制器:
class SessionsController < Devise::SessionsController
respond_to :json
def create
logger.debug(" CUSTOM AUTHENTICATION!!!!!!")
self.resource = warden.authenticate!(:scope => resource_name, :recall => "#{controller_path}#failure")
sign_in(resource_name, resource)
render json: { :success => true }, status: 200
end
def failure
logger.debug(" CUSTOM FAILURE!!!!!!")
warden.custom_failure!
render json: { success: false, errors: ["Login Credentials Failed"] }, status: 401
end
end
我正在調用槽Ajax,並且需要此成功參數才能處理回調。
身份驗證正常。 我正在收到success: true
作為回應,這是success: true
但是,當身份驗證失敗時,我將收到默認響應:
"NetworkError: 401 Unauthorized - http://localhost:3000/users/sign_in.json"
Parameters:
{"error":"Invalid e-mail or password"}
我在那里想念什么? 為什么devise不會調用我的自定義失敗消息?
我已經配置了devise.rb配置:
config.navigational_formats = ["*/*", :html, :json]
您正在修補Devise的SessionsController-您可以在此處看到該控制器在SessionsController中沒有失敗方法。 401正在通過新方法生成。 但是,除了修補該方法之外,我將使用after_filter像這樣:
class SessionsController < Devise::SessionsController
after_filter :handle_failed_login, :only => :new
private
def handle_failed_login
if failed_login?
render json: { success: false, errors: ["Login Credentials Failed"] }, status: 401
end
end
def failed_login?
(options = env["warden.options"]) && options[:action] == "unauthenticated"
end
end
您只需要編輯devise配置並更改config.navigational_formats,然后使用:format =>:json更改路徑
config.navigational_formats = ['*/*', :html, :json]
api_v1_new_user_session_path(resource_name, :format => :json)
如果您進行此更改,:recall將起作用
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.