Search code examples
ruby-on-railsrubydevise

Rails 7 Encryption Email + Devise (ERROR)


I'm using Rails 7 + Devise gem. I'm trying to encrypt the email field (generated by devise). I successfully did rails db:encryption:init and set my credentials.

User Model:

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

end

When I try to visit both sign in and sign up path, I get these errors:

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

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

Both point to this line in the view:

<%= f.input :email,

Even when I try to create a new user through the rails console, I get:

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

How can I solve this? Is the new Rails 7 ActiveRecord Encryption available with Devise?

Thank you very much.


Solution

  • Looks like this should help with your error https://github.com/heartcombo/devise/issues/5436#issuecomment-1014152052

    There are 2 approaches, you can set:

    config.active_record.encryption.support_unencrypted_data = true
    

    or change migration for email field:

    null: false, default: ""