2012-02-10 31 views
17

在啓用可確認模塊後,Devise將不允許未經確認的用戶在預定義的時間段過後登錄。相反,用戶會被重定向回登錄頁面,並顯示「您必須在繼續之前確認您的帳戶」的消息。用確認設計 - 當用戶嘗試使用未經確認的電子郵件登錄時將用戶重定向到自定義頁面

這是一個不受歡迎的交互模式,因爲閃光通知沒有提供足夠的空間來向用戶正確解釋訪問被拒絕的原因,「確認您的帳戶」的含義,提供重新發送確認的鏈接以及說明關於如何檢查您的垃圾郵件文件夾等。

有沒有一種方法可以將此行爲改爲重定向到特定的URL?

回答

27

對不起,我以爲你的意思是在註冊後沒有登錄。所以下面的下跌工程如何引導用戶申請後,您需要登錄到做是創建一個自定義的設計是什麼:: FailureApp

見wiki頁面:那麼您的自定義FailureApp內https://github.com/plataformatec/devise/wiki/How-To:-Redirect-to-a-specific-page-when-the-user-can-not-be-authenticated

https://github.com/plataformatec/devise/blob/master/lib/devise/failure_app.rb覆蓋redirect_url方法:

def redirect_url 
    if warden_message == :unconfirmed 
     custom_redirect_path 
    else 
     super 
    end 
    end 

對於自定義重定向登錄後,最多:

還有就是RegistrationsContro內的控制器方法after_inactive_sign_up_path_for你可以覆蓋來完成這一點。

首先在你的路線,你需要指定要使用自定義控制器:

config/routes.rb

devise_for :users, :controllers => { :registrations => "users/registrations" } 

第二你創建一個從正常的控制器繼承,以覆蓋方法您的自定義控制器:

app/controllers/users/registrations_controller.rb

class Users::RegistrationsController < Devise::RegistrationsController 

    protected 

    def after_inactive_sign_up_path_for(resource) 
    signed_up_path 
    end 

end 

在這種情況下,對於我的應用程序,我的設計模型是用戶,所以如果您的模型名稱不同,您可能需要更改該名稱空間。我希望我的用戶被重定向到signed_up_path,但您可以將其更改爲您所需的路徑。

9

我剛剛做了這個,但採取了不同的方法。

在app /控制器/ sessions_controller.rb:

class SessionsController < Devise::SessionsController 

    before_filter :check_user_confirmation, only: :create 

    # 
    # other code here not relevant to the example 
    # 

private 

    def check_user_confirmation 
    user = User.find_by_email(params[:email]) 
    redirect_to new_confirmation_path(:user) unless user && user.confirmed? 
    end 
end 

這個工作對我來說,似乎微創。在我的應用程序中,新會話總是需要經過sessions#create,用戶始終使用他們的電子郵件地址登錄,因此這可能比您的更簡單。

您當然可以在check_user_confirmation方法中使用您想要的任何位置。 new_confirmation_path對我來說是合乎邏輯的選擇,因爲它爲用戶提供了資源以得到確認。

+0

一個問題:我如何才能找到時,他可以用用戶名或郵箱登錄用戶?所以你得到':login'作爲參數,它可以是用戶的電子郵件或用戶名。 – jonhue 2017-01-22 10:33:22

0

這是我需要添加的解決方案:在會話下的設計語言環境中未經確認的消息。

在app /控制器/ sessions_controller.rb

def check_user_confirmation 
    user = User.where(email: params[:user][:email]).take 

    unless user && user.confirmed? 
     set_flash_message! :alert, :unconfirmed 
     expire_data_after_sign_in! 
     respond_with user, location: after_inactive_sign_up_path_for(user) 
    end 
    end 

    protected 

    def after_inactive_sign_up_path_for(resource) 
    new_user_session_path 
    end 
相關問題