How redirect 'Confirmation token invalid' on Devise

醉酒当歌 提交于 2019-12-11 02:34:34

问题


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)

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

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, my user model is configured in this way:

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

回答1:


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

Just replace this line

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

with this

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

That's it.



来源:https://stackoverflow.com/questions/18261840/how-redirect-confirmation-token-invalid-on-devise

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!