[英]Devise with Confirmable - Redirect user to a custom page when users tries to sign in with an unconfirmed email
啟用可確認模塊后,Devise將不允許未經確認的用戶在預定義的時間段過后登錄。 而是將用戶重定向回登錄頁面,並顯示“您必須先確認帳戶才能繼續”。
這是一種不受歡迎的交互模型,因為閃存通知沒有提供足夠的空間來向用戶正確解釋訪問被拒絕的原因,“確認您的帳戶”的含義,提供重新發送確認的鏈接以及如何檢查的說明您的垃圾郵件文件夾等。
有沒有辦法可以更改此行為以重定向到特定的URL?
抱歉,一開始我認為你的意思是注冊后沒有登錄。 因此,下面的內容適用於如何在注冊后引導用戶以及您需要為登錄做什么就是創建自定義Devise :: FailureApp
請參閱維基頁面: https : //github.com/plataformatec/devise/wiki/How-To : -Redirect- to-a-specific-page- when- the-user-can-not-be- authenticated
然后在您的自定義FailureApp覆蓋redirect_url
方法,從https://github.com/plataformatec/devise/blob/master/lib/devise/failure_app.rb :
def redirect_url
if warden_message == :unconfirmed
custom_redirect_path
else
super
end
end
注冊后的自定義重定向:
在RegistrationsController中有一個控制器方法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
在這種情況下,對於我的App,我的Devise模型是User,因此如果模型的命名方式不同,您可能希望更改該命名空間。 我希望將我的用戶重定向到signed_up_path
,但您可以將其更改為所需的路徑。
我剛剛做了這個,但采取了不同的方法。
在app / controllers / 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
方法中redirect_to
您想要的任何位置。 new_confirmation_path
對我來說是合乎邏輯的選擇,因為它為用戶提供了獲得確認的資源。
這是我需要添加的解決方案:會話下方的設計區域設置上的未確認消息。
在app / controllers / 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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.