Sha256: 408f6ae9cc398dedafd337ccf26fbba8d6b5c03e9d37785dfa40bcac86610835

Contents?: true

Size: 1.47 KB

Versions: 1

Compression:

Stored size: 1.47 KB

Contents

class <%= class_name %> < ApplicationRecord
  has_secure_password

  has_many :sessions, dependent: :destroy
  <%- if options.trackable? -%>
  has_many :events, dependent: :destroy
  <%- end -%>
  <%- if code_verifiable? %>
  kredis_string :verification_code, expires_in: 2.days
  <%- end -%>

  validates :email, presence: true, uniqueness: true
  validates_format_of :email, with: /\A[^@\s]+@[^@\s]+\z/

  validates_length_of :password, minimum: 12, allow_nil: true
  validates_format_of :password, with: /(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])/, allow_nil: true, message: "might easily be guessed"
  <%- if options.pwned? -%>
  validates :password, not_pwned: { message: "might easily be guessed" }
  <%- end -%>

  before_validation do
    self.email = email.downcase.strip
  end

  before_validation if: :email_changed? do
    self.verified = false
  end

  after_update if: :password_digest_previously_changed? do
    sessions.where.not(id: Current.session).destroy_all
  end

  after_save_commit if: :email_previously_changed? do
    IdentityMailer.with(user: self).email_verify_confirmation.deliver_later
  end
  <%- if options.trackable? %>
  after_save_commit if: :email_previously_changed? do
    events.create! action: "email_verification_requested"
  end

  after_update if: :password_digest_previously_changed? do
    events.create! action: "password_changed"
  end

  after_update if: :verified_previously_changed? do
    events.create! action: "email_verified" if verified?
  end
  <%- end -%>
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
authentication-zero-2.11.2 lib/generators/authentication/templates/models/model.rb.tt