设计确认_token无效

Wor*_*ica 8 ruby-on-rails devise

我的User.rb:

class User < ActiveRecord::Base

    devise :database_authenticatable, :registerable,:confirmable,:token_authenticatable,
     :recoverable, :rememberable, :trackable, :validatable, :authentication_keys => [:name]
Run Code Online (Sandbox Code Playgroud)

我的路线:

devise_for :users, :controllers => { :sessions => "sessions", :confirmations => "confirmations", :passwords => "passwords", :registrations => "registrations" }
Run Code Online (Sandbox Code Playgroud)

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

我的电子邮件链接如下:

/users/confirmation?confirmation_token=167bad44a15e02b0bd570b51e1bf927b88368d8855d92b9833a24017a2bad4be
Run Code Online (Sandbox Code Playgroud)

在数据库用户有

confirmation_token:167bad44a15e02b0bd570b51e1bf927b88368d8855d92b9833a24017a2bad4be
Run Code Online (Sandbox Code Playgroud)

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

Resend confirmation instructions
 Confirmation token is invalid
Run Code Online (Sandbox Code Playgroud)

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

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
Run Code Online (Sandbox Code Playgroud)

我说"标准控制器",因为当我删除它而不使用自定义控制器问题是一样的.

gre*_*tes 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)
Run Code Online (Sandbox Code Playgroud)

如您所见,您通过查询字符串参数传递的令牌由该消息使用Devise.token_generator,并且该操作的结果是与数据库中的令牌进行比较以发现用户记录的结果.

看起来暂时可以(在3.1但不是3.2)通过设置关闭它

config.allow_insecure_token_lookup = true
Run Code Online (Sandbox Code Playgroud)

在你的设计初始化程序中.但是默认行为已经改变,以使设计更安全.有关设计3.1中安全性改进的完整概述,请参阅此博客文章,包括此更改.


Eri*_*man 5

您可以使用下面的解决方案(设置config.allow_insecure_token_lookup = true),但根据Devise更改日志,这将仅暂时可用.

问题很可能是因为您运行了设计生成器,在进行这些更改之前将所有视图转储回您的视图中.然后你更新了你的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>
Run Code Online (Sandbox Code Playgroud)

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

主要区别是@resource.confirmation_token变得公正@token.