ruby-on-rails - 为可确认设计生成自定义令牌
问题描述
我正在使用 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
解决方案
在创建回调之前设计使用来生成令牌。
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
检查此模块以获取更多信息。
推荐阅读
- android - 在 web 视图中使用服务人员和自签名证书加载 url 时出错
- obiee - 使用 obiee 中的参数打开网页
- flutter - 我正在尝试制作一个颤动的应用程序来控制我的飞利浦 Hue 灯,但我遇到了 hue_dart 问题
- c++ - Qt - 跟踪 QAbstractAnimation 运动
- javascript - 根据原始数组索引是奇数还是偶数创建一个新数组
- javascript - client.channels.get 不是 discord.js 中的函数
- node.js - 使用节点 js SDK 或 API 将附件添加到 Xero 中的发票
- javascript - 如何将此单个幻灯片脚本转换为多个幻灯片
- javascript - 有没有像 Promise.all() 这样的函数,但即使在一个失败之后也会运行所有的 Promise?
- flutter - 多次执行语句