Sha256: cccf0d640362f50c683a1a873214a38fff132743490a5c8b5befa85b99719bea

Contents?: true

Size: 1.41 KB

Versions: 4

Compression:

Stored size: 1.41 KB

Contents

class User < ApplicationRecord
  has_secure_password

  has_many :email_verification_tokens, dependent: :destroy
  has_many :password_reset_tokens, dependent: :destroy
  <%- if two_factor? -%>
  has_many :recovery_codes, dependent: :destroy
  <%- end -%>
  <%- if passwordless? -%>
  has_many :sign_in_tokens, dependent: :destroy
  <%- end -%>

  has_many :sessions, dependent: :destroy
  <%- if options.trackable? -%>
  has_many :events, dependent: :destroy
  <%- end -%>

  validates :email, presence: true, uniqueness: true, format: { with: URI::MailTo::EMAIL_REGEXP }
  validates :password, allow_nil: true, length: { minimum: 12 }
  <%- if options.pwned? -%>
  validates :password, not_pwned: { message: "might easily be guessed" }
  <%- end -%>

  before_validation if: -> { email.present? } do
    self.email = email.downcase.strip
  end

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

  after_update if: :password_digest_previously_changed? do
    sessions.where.not(id: Current.session).delete_all
  end
  <%- if options.trackable? %>
  after_update 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?, :verified?] do
    events.create! action: "email_verified"
  end
  <%- end -%>
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
authentication-zero-2.16.19 lib/generators/authentication/templates/models/user.rb.tt
authentication-zero-2.16.18 lib/generators/authentication/templates/models/user.rb.tt
authentication-zero-2.16.17 lib/generators/authentication/templates/models/user.rb.tt
authentication-zero-2.16.16 lib/generators/authentication/templates/models/user.rb.tt