LoginSignup
1
1

More than 3 years have passed since last update.

メモ:Deviseのメールの認証トークンの長さを変更する

Posted at

この記事は

  • Deviseの超細かい小技のメモです

やりたかったこと

  • Deviseにはユーザ仮登録メールやパスワード再発行メールを送信する機能があります(ConfirmableとかRememberableとか)
  • これらのメールには認証トークン付きのリンクが記載されていて、これをクリックすることで本人確認が実施される構成になっています
  • やりたかったのは、セキュリティのレギュレーション上、この認証トークンの長さを32文字にしたかったです
    • デフォルトは20文字

対応方法

  • 色々調べましたが設定はありません
  • この辺のメソッドで文字列生成されています
  • 仕方がないのでモンキーパッチ当てて解決しました
config/initializers/devise_ext.rb
module Devise
  def self.friendly_token(length = 32)
    # To calculate real characters, we must perform this operation.
    # See SecureRandom.urlsafe_base64
    rlength = (length * 3) / 4
    SecureRandom.urlsafe_base64(rlength).tr('lIO0', 'sxyz')
  end
end

終わりに

  • せっかくパラメータをオプションにしてるんだから、どこかで設定できるようにしてほしいなー
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1