2013-02-07 29 views
3

我正在使用omniauthomniauth-identity通過Google,Facebook和傳統的用戶名/密碼進行身份驗證。除了身份驗證失敗的情況之外,我已經很好地工作了。例如,在輸入無效密碼失敗時,omniauth將呼叫/auth/failure。我已將其映射到控制器,該控制器重定向到適當的頁面並呈現閃光消息。我有的問題是我無法讓閃光燈實際顯示。示例代碼:Omniauth身份驗證失敗回調處理

routes.rb

match "/auth/failure" => "sessions#failure" 

在它調用控制器:

def failure 
    redirect_to root_url, alert: "Authentication failed, please try again." 
end 

我覺得閃光燈正在丟失,由於任何omniauth是幹什麼的組合和重定向。我知道視圖代碼是正確的,因爲它會閃爍其他東西,如成功的登錄消息。我將不勝感激關於如何調整我的示例以獲取顯示的Flash消息的建議,或針對備用omniauth失敗處理機制的建議。謝謝。

回答

-2

我在我上一個項目中遇到同樣的問題。這是一個錯誤。添加以下猴子補丁到你的config/initializers/omniauth.rb

# Omniauth failure monkey patch 
    on_failure do |env| 
    message_key = env['omniauth.error.type'] 
    origin_query_param = env['omniauth.origin'] ? "&origin=#{CGI.escape(env['omniauth.origin'])}" : "" 
    strategy_name_query_param = env['omniauth.error.strategy'] ? "&strategy=#{env['omniauth.error.strategy'].name}" : "" 
    extra_params = env['omniauth.params'] ? "&#{env['omniauth.params'].to_query}" : "" 
    new_path = "#{env['SCRIPT_NAME']}#{OmniAuth.config.path_prefix}/failure?message=#{message_key}#{origin_query_param}#{strategy_name_query_param}#{extra_params}" 
    Rack::Response.new(["302 Moved"], 302, 'Location' => new_path).finish 
    end 
+0

謝謝。也許我誤解了如何應用它,但它的行爲似乎與我目前的行爲沒有什麼不同([omniauth FAQ](https://github.com/intridea/omniauth/wiki/FAQ)中的'redirect_after_failure') ) - 沒有閃光消息。 – SingleShot