设计确认_token无效

时间:2013-08-22 20:20:29

标签: ruby-on-rails devise

我的User.rb:

class User < ActiveRecord::Base

    devise :database_authenticatable, :registerable,:confirmable,:token_authenticatable,
     :recoverable, :rememberable, :trackable, :validatable, :authentication_keys => [:name]

我的路线:

devise_for :users, :controllers => { :sessions => "sessions", :confirmations => "confirmations", :passwords => "passwords", :registrations => "registrations" }

我的ConfirmationsController是一个标准控制器,但具有不同的重定向。

我的电子邮件上有链接:

/users/confirmation?confirmation_token=167bad44a15e02b0bd570b51e1bf927b88368d8855d92b9833a24017a2bad4be

在数据库用户中有

confirmation_token:167bad44a15e02b0bd570b51e1bf927b88368d8855d92b9833a24017a2bad4be

但是当我点击该链接时,我只会看到以下页面:

Resend confirmation instructions
 Confirmation token is invalid

我不做什么 - 我还需要设置什么。

CONFIRMATIONCONTROLLER:

def resource_params
 params.require(:user).permit(:confirmation_token)
   end
   private :resource_params


  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)
  session['new_user'] = true
  respond_with_navigational(resource){ redirect_to after_confirmation_path_for(resource_name, resource) }
else
  respond_with_navigational(resource.errors, :status => :unprocessable_entity){ render :new }
end
  end

  protected
    # The path used after resending confirmation instructions.
    def after_resending_confirmation_instructions_path_for(resource_name)
      new_registration_path(resource_name)
    end

我说“标准控制器”,因为当我删除它并且不使用自定义控制器时问题就是这样。

3 个答案:

答案 0 :(得分:17)

您使用的是哪个版本的设计?如果您处于3.1.0或更高级别,则会出现此行为:

CHANGELOG.md

存储在数据库中的令牌不应与您在确认电子邮件中发送的令牌相匹配。请参阅devise/lib/devise/models/confirmable.rb,其中包含以下内容:

def confirm_by_token(confirmation_token)
  original_token     = confirmation_token
  confirmation_token = Devise.token_generator.digest(self, :confirmation_token, confirmation_token)

  confirmable = find_or_initialize_with_error_by(:confirmation_token, confirmation_token)

正如您所看到的,您通过查询字符串参数传递的令牌由Devise.token_generator使用,该操作的结果是与数据库中的令牌进行比较以发现用户记录。< / p>

看起来暂时可以(在3.1而不是3.2)通过设置

来关闭它
config.allow_insecure_token_lookup = true

在您的设计初始化程序中。但是默认行为已经改变,以使设计更安全。有关设计3.1中安全性改进的完整概述,请参阅this blog post,包括此更改。

答案 1 :(得分:5)

您可以使用下面的解决方案(设置config.allow_insecure_token_lookup = true),但根据the Devise changelog,这只能暂时使用。

问题很可能是因为您运行了设计生成器,以便在进行这些更改之前将所有视图转储回您的视图中。然后你更新了你的Devise gem并改变了所有后端的东西,但你的观点并没有改变。现在,您所有与设计相关的视图和邮件都已过时,并且无法使用新的令牌样式。

您可以在https://github.com/plataformatec/devise/tree/master/app/views/devise/mailer看到新的电子邮件。主要变化是改变这一行:

<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>

主要区别在于@resource.confirmation_token变为@token

答案 2 :(得分:3)

我将@resource.confirmation_token更改为@token然后就可以了。