简体   繁体   English

使用可确认设计 - 当用户尝试使用未经证实的电子邮件登录时,将用户重定向到自定义页面

[英]Devise with Confirmable - Redirect user to a custom page when users tries to sign in with an unconfirmed email

With the Confirmable module enabled, Devise will not allow an unconfirmed user to sign in after a predefined period of time has elapsed. 启用可确认模块后,Devise将不允许未经确认的用户在预定义的时间段过后登录。 Instead the user is redirected back to the sign in page with the flash message "You have to confirm your account before continuing". 而是将用户重定向回登录页面,并显示“您必须先确认帐户才能继续”。

This is an undesirable interaction model, as a flash notice does not provide adequate space to properly explain to the user why access has been denied, what "confirm your account" means, provide a link to resend the confirmation, and instructions on how to check your spam folder and so on. 这是一种不受欢迎的交互模型,因为闪存通知没有提供足够的空间来向用户正确解释访问被拒绝的原因,“确认您的帐户”的含义,提供重新发送确认的链接以及如何检查的说明您的垃圾邮件文件夹等。

Is there a way I can change this behaviour to redirect to a specific URL instead? 有没有办法可以更改此行为以重定向到特定的URL?

Sorry at first I thought you meant after Sign Up not Sign In. 抱歉,一开始我认为你的意思是注册后没有登录。 So the down below works for how to direct users after Sign Up and what you need to do for Sign In is to create a custom Devise::FailureApp 因此,下面的内容适用于如何在注册后引导用户以及您需要为登录做什么就是创建自定义Devise :: FailureApp

See the wiki page: 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/wiki/How-To : -Redirect- to-a-specific-page- when- the-user-can-not-be- authenticated

Then within your custom FailureApp overwrite redirect_url method from https://github.com/plataformatec/devise/blob/master/lib/devise/failure_app.rb : 然后在您的自定义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

For custom redirect after Sign Up: 注册后的自定义重定向:

There is a controller method after_inactive_sign_up_path_for within the RegistrationsController that you can overwrite to accomplish this. 在RegistrationsController中有一个控制器方法after_inactive_sign_up_path_for ,您可以覆盖它来完成此操作。

First in your Routes you will need to specify to use your custom controller: 首先在路由中,您需要指定使用自定义控制器:

config/routes.rb : config/routes.rb

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

Second you create your custom controller that inherits from the normal controller in order to overwrite the method: 其次,您创建从普通控制器继承的自定义控制器,以覆盖该方法:

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

In this case for my App my Devise model is User so you may want to change that namespace if your model is named differently. 在这种情况下,对于我的App,我的Devise模型是User,因此如果模型的命名方式不同,您可能希望更改该命名空间。 I wanted my users to be redirected to the signed_up_path , but you can change that to your desired path. 我希望将我的用户重定向到signed_up_path ,但您可以将其更改为所需的路径。

I just did this, but took a different approach. 我刚刚做了这个,但采取了不同的方法。

in app/controllers/sessions_controller.rb: 在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

This worked for me and seemed minimally invasive. 这对我有用,似乎微创。 In my app new sessions always have to go through sessions#create and users always sign in with their email address, so this may be a simpler case than yours. 在我的应用程序中,新会话始终必须通过sessions#create和用户始终使用他们的电子邮件地址登录,因此这可能比您的更简单。

You can of course redirect_to any location you desire in the check_user_confirmation method. 您当然可以在check_user_confirmation方法中redirect_to您想要的任何位置。 new_confirmation_path was the logical choice for me because it provides users with the resources to get confirmed. new_confirmation_path对我来说是合乎逻辑的选择,因为它为用户提供了获得确认的资源。

This is my solution you need to add :unconfirmed message on devise locales below the sessions. 这是我需要添加的解决方案:会话下方的设计区域设置上的未确认消息。

in app/controllers/sessions_controller.rb 在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.

相关问题 设计:将未确认的电子邮件地址传回登录页面 - Devise: Pass unconfirmed email address back to sign in page 注册后使用Devise + Confirmable将用户重定向到特定页面 - Redirect user to certain page after registration using Devise + confirmable Rails,Devise:如果匿名用户尝试访问页面-重定向用户以登录页面 - Rails, Devise: If an anonymous user tries to access a page - redirect user to sign in page Rails + Devise:当用户在通过确认电子邮件进行确认之前尝试登录时,没有错误提示出现吗? - Rails + Devise: When a user tries to sign in before confirming with the confirmation email, no error notice appears? 测试设计after_inactive_sign_up_path_以获得可确认的重定向 - Test devise after_inactive_sign_up_path_for confirmable redirect Rails Devise Confirmable-重定向 - Rails Devise Confirmable - redirect 仅设计电子邮件注册不显示确认视图 - Devise email only sign up not showing confirmable view 如何将未经确认的用户的电子邮件传递给会话(Devise)? - How can I pass email of unconfirmed user to the session (Devise)? 设计-如何在验证页面上允许更改未确认的电子邮件(更正) - Devise - how to allow change of unconfirmed email (correction) on verification page 设计 - 当用户更新失败重定向到自定义页面时 - Devise - when User Update failure redirect to custom page
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM