首页 > 解决方案 > 为可确认设计生成自定义令牌

问题描述

我正在使用 devise:confirmable,并且想要使用自定义令牌,因为我需要在确认令牌本身中附加一些数据。如何配置?

用户模型:

class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable,
         :confirmable, :password_archivable

标签: ruby-on-railsoauth-2.0devisedevise-confirmable

解决方案


在创建回调之前设计使用来生成令牌。

before_create :generate_confirmation_token, if: :confirmation_required?

您可以覆盖模型generate_confirmation_token中设计的方法User

波纹管是该方法的默认行为。

    # Generates a new random token for confirmation, and stores
    # the time this token is being generated in confirmation_sent_at
    def generate_confirmation_token
      if self.confirmation_token && !confirmation_period_expired?
        @raw_confirmation_token = self.confirmation_token
      else
        self.confirmation_token = @raw_confirmation_token = Devise.friendly_token
        self.confirmation_sent_at = Time.now.utc
      end
    end

检查此模块以获取更多信息。


推荐阅读