Sha256: 6a714ec4fefc78432dfedd86ea3363a7800841fbf9b8d0bff773b6d72491a77e

Contents?: true

Size: 1.49 KB

Versions: 4

Compression:

Stored size: 1.49 KB

Contents

class User < ApplicationRecord
  has_secure_password

  has_one :email_verification_token, dependent: :destroy
  has_one :password_reset_token, dependent: :destroy

  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, format: { with: URI::MailTo::EMAIL_REGEXP }
  validates :password, allow_nil: true, length: { minimum: 12 }, format: { with: /(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])/ }
  <%- if options.pwned? -%>
  validates :password, not_pwned: { message: "might easily be guessed" }
  <%- end -%>

  before_validation do
    self.email = email.try(:downcase).try(: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
    UserMailer.with(user: self).email_verification.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

4 entries across 4 versions & 1 rubygems

Version Path
authentication-zero-2.15.1 lib/generators/authentication/templates/models/user.rb.tt
authentication-zero-2.15.0 lib/generators/authentication/templates/models/user.rb.tt
authentication-zero-2.14.0 lib/generators/authentication/templates/models/user.rb.tt
authentication-zero-2.13.0 lib/generators/authentication/templates/models/user.rb.tt