使用可确认设计 - 当用户尝试使用未经确认的电子邮件登录时,将用户重定向到自定义页面 [英] Devise with Confirmable - Redirect user to a custom page when users tries to sign in with an unconfirmed email

查看:11
本文介绍了使用可确认设计 - 当用户尝试使用未经确认的电子邮件登录时,将用户重定向到自定义页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

启用可确认模块后,Devise 将不允许未经确认的用户在经过预定义的时间段后登录.而是将用户重定向回登录页面,并显示闪现消息您必须在继续之前确认您的帐户".

With the Confirmable module enabled, Devise will not allow an unconfirmed user to sign in after a predefined period of time has elapsed. 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.

有什么办法可以改变这种行为以重定向到特定的 URL 吗?

Is there a way I can change this behaviour to redirect to a specific URL instead?

推荐答案

抱歉一开始我以为你是说注册而不是登录.因此,下面的内容适用于如何在注册后引导用户以及您需要为登录做的是创建一个自定义的 Devise::FailureApp

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

查看维基页面:https://github.com/plataformatec/devise/wiki/How-To:-Redirect-to-a-specific-page-when-the-user-can-not- 认证

然后在您的自定义FailureApp 中覆盖redirect_url 方法"noreferrer">https://github.com/plataformatec/devise/blob/master/lib/devise/failure_app.rb:

Then within your custom FailureApp overwrite redirect_url method from 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:

在 RegistrationsController 中有一个控制器方法 after_inactive_sign_up_path_for,您可以覆盖它来完成此操作.

There is a controller method after_inactive_sign_up_path_for within the RegistrationsController that you can overwrite to accomplish this.

首先在你的路由中你需要指定使用你的自定义控制器:

First in your Routes you will need to specify to use your custom controller:

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

在这种情况下,对于我的应用程序,我的设计模型是用户,因此如果您的模型命名不同,您可能需要更改该命名空间.我希望我的用户被重定向到 signed_up_path,但您可以将其更改为您想要的路径.

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. I wanted my users to be redirected to the signed_up_path, but you can change that to your desired path.

这篇关于使用可确认设计 - 当用户尝试使用未经确认的电子邮件登录时,将用户重定向到自定义页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆