在Devise上如何重定向“确认令牌无效” [英] How redirect 'Confirmation token invalid' on Devise

查看:214
本文介绍了在Devise上如何重定向“确认令牌无效”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用devise配置了我的Rails应用程序:可以确认,注册后用户收到一封确认链接的电子邮件。当用户第二次单击此链接令牌无效,这是我的应用程序中的预期行为。但是,它将用户发送到位于app \views\devise\confirmations\\\
n.html.html的页面,并显示错误确认令牌无效,而我想要显示该错误(< ;%= devise_error_messages!%>),但在我的登录页面。 (实际上有两种可能的状态:a)令牌过期,用户未被确认,因此用户必须再次要求一个令牌; b)令牌过期,用户被确认,所以用户必须刚刚登录)

I configured my Rails application with devise :confirmable and after registration the user receives an email with the confirmation link. When the user click in this link a second time the token is invalid which is the expected behavior in my application. However, it sends the user to a page located at "app\views\devise\confirmations\new.html.erb" with an error 'Confirmation token is invalid' and what I want is to show that error(<%= devise_error_messages! %>), but in my Sign In Page. (Actually, there are two possible states: a) The token expired and the user is NOT confirmed so the user must ask for a token once again; b) The token expired and the user IS confirmed so the user must just sign in)

为此,我继承了Devise :: ConfirmationsController创建我自己的ConfirmationsController以覆盖show method,所以我可以重定向到登录页面,如果有任何错误如下:

For this, I inherited the Devise::ConfirmationsController creating my own ConfirmationsController to override the show method so I can redirect to sign in page if there is any error as follows:

class ConfirmationsController  < Devise::ConfirmationsController

  def after_confirmation_path_for(resource_name, resource)
    super
  end

  def new
    super
  end

  def after_resending_confirmation_instructions_path_for(resource_name)
    super
  end

  def create
    super
  end

  def show
    self.resource = resource_class.confirm_by_token(params[:confirmation_token])

    if resource.errors.empty?
      set_flash_message(:notice, :confirmed) if is_navigational_format?
      sign_in(resource_name, resource)
      respond_with_navigational(resource){ redirect_to
            after_confirmation_path_for(resource_name, resource) }
    else
        respond_with_navigational(resource.errors, :status => :unprocessable_entity){ render 'devise/sessions/new'  }
    end
  end

end

在我的routes.rb中我有这样的配置:

And in my routes.rb I have this configuration:

get "confirmations/show"

devise_for :users, :path => '', :path_names => {:sign_in => 'login', :sign_out => 'logout'}, :controllers => {:confirmations => 'confirmations'}
(...)

它重定向到登录页面,但我无法将资源中的错误传递到登录页面,无论如何我尝试。如何解决这个问题?

It redirects properly to the sign in page, but I can't pass the error(s) present in resources.errors to the sign in page doesn't matter how hard I try. How can I solve this problem?

Btw,我的用户模型是这样配置的:

Btw, my user model is configured in this way:

devise :database_authenticatable, :registerable, :confirmable,
    :recoverable, :rememberable, :trackable, :validatable


推荐答案

您唯一需要做的是confirm_instructions.html.erb中的更改

The only thing you need to do is change in confirmation_instructions.html.erb

只需替换这行

<p><%= link_to 'Confirm my account', confirmation_url(@resource, :confirmation_token =>  @resource.confirmation_token) %></p>

与此

<p><%= link_to 'Confirm my account', confirmation_url(@resource, :confirmation_token => @token) %></p>

就是这样。

这篇关于在Devise上如何重定向“确认令牌无效”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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