Techioz Blog

Rails 7 暗号化電子メール + デバイス (エラー)

概要

Rails 7 + Devise gemを使用しています。電子メールフィールド(デバイスによって生成された)を暗号化しようとしています。 Rails db:encryption:init を正常に実行し、資格情報を設定しました。

ユーザーモデル:

class User < ApplicationRecord
  
  encrypts :email, deterministic: true, downcase: true
  validates :email, presence: true
  
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :validatable, :confirmable

end

サインイン パスとサインアップ パスの両方にアクセスしようとすると、次のエラーが発生します。

ActiveRecord::Encryption::Errors::Decryption in Devise::Sessions#new

ActiveRecord::Encryption::Errors::Decryption in Devise::Registrations#new

どちらもビュー内の次の行を指します。

<%= f.input :email,

Rails コンソールから新しいユーザーを作成しようとしても、次のようなメッセージが表示されます。

unexpected token at '' (JSON::ParserError) 
ActiveRecord::Encryption::Errors::Encoding (ActiveRecord::Encryption::Errors::Encoding)
ActiveRecord::Encryption::Errors::Decryption (ActiveRecord::Encryption::Errors::Decryption)

どうすればこれを解決できますか?新しい Rails 7 ActiveRecord Encryption は Devise で利用できますか?

どうもありがとうございます。

解決策

これはエラーの解決に役立つようですhttps://github.com/heartcombo/devise/issues/5436#issuecomment-1014152052

2 つのアプローチがあり、次のように設定できます。

config.active_record.encryption.support_unencrypted_data = true

または、メールフィールドの移行を変更します。

null: false, default: ""