-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ad5996b
commit 97d5856
Showing
3 changed files
with
66 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
# frozen_string_literal: true | ||
|
||
module PagesCore | ||
module AuthenticableUser | ||
extend ActiveSupport::Concern | ||
|
||
included do | ||
has_secure_password | ||
|
||
validates(:otp_secret, presence: true, if: :otp_enabled?) | ||
validates( | ||
:password, | ||
length: { | ||
minimum: 8, | ||
maximum: ActiveModel::SecurePassword::MAX_PASSWORD_LENGTH_ALLOWED, | ||
allow_blank: true | ||
} | ||
) | ||
|
||
before_save :update_session_token | ||
end | ||
|
||
module ClassMethods | ||
def authenticate(email, password:) | ||
User.find_by_email(email).try(:authenticate, password) | ||
end | ||
end | ||
|
||
def authenticate!(password) | ||
return false unless can_login? && valid_password?(password) | ||
|
||
rehash_password!(password) if password_needs_rehash? | ||
true | ||
end | ||
|
||
def can_login? | ||
activated? | ||
end | ||
|
||
def recovery_codes=(codes) | ||
self.hashed_recovery_codes = codes.map do |c| | ||
BCrypt::Password.create(c, cost: 8) | ||
end | ||
end | ||
|
||
def use_recovery_code!(code) | ||
valid_hashes = hashed_recovery_codes.select do |c| | ||
BCrypt::Password.new(c) == code | ||
end | ||
return false unless valid_hashes.any? | ||
|
||
update(hashed_recovery_codes: hashed_recovery_codes - valid_hashes) | ||
true | ||
end | ||
|
||
private | ||
|
||
def update_session_token | ||
return unless !session_token? || password_digest_changed? || | ||
otp_enabled_changed? | ||
|
||
self.session_token = SecureRandom.hex(32) | ||
end | ||
end | ||
end |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters