Sha256: 93e0b8020df868d0d1979324cc3ed5541e080f35f76aa1670368033acdf02d6b

Contents?: true

Size: 1.38 KB

Versions: 3

Compression:

Stored size: 1.38 KB

Contents

class <%= class_name %> < ApplicationRecord
  has_secure_password

  has_many :sessions, dependent: :destroy
  <%- if options.trackable? -%>
  has_many :events, dependent: :destroy
  <%- 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

3 entries across 3 versions & 1 rubygems

Version Path
authentication-zero-2.11.1 lib/generators/authentication/templates/models/model.rb.tt
authentication-zero-2.11.0 lib/generators/authentication/templates/models/model.rb.tt
authentication-zero-2.10.0 lib/generators/authentication/templates/models/model.rb.tt