Class: NoPassword::Session::Authentication
- Inherits:
-
Object
- Object
- NoPassword::Session::Authentication
- Includes:
- ActiveModel::Attributes, ActiveModel::Model
- Defined in:
- lib/nopassword/session/authentication.rb
Overview
Base authentication class that manages return_url and wraps Link challenge/verification. Subclass this to add identifier validation (e.g., Email::Authentication adds email validation).
Direct Known Subclasses
Class Method Summary collapse
-
.update(session) ⇒ Object
Update session data, optionally yielding for modifications.
Instance Method Summary collapse
- #challenge ⇒ Object
- #delete ⇒ Object
-
#identifier ⇒ Object
Override in subclass to return the identifier (email, phone, etc.).
-
#initialize(session, **attributes) ⇒ Authentication
constructor
A new instance of Authentication.
- #persisted? ⇒ Boolean
- #save ⇒ Object
- #save! ⇒ Object
-
#save_without_validation ⇒ Object
Save without running validations - useful for syncing from challenge.
- #verification(token:) ⇒ Object
Constructor Details
#initialize(session, **attributes) ⇒ Authentication
Returns a new instance of Authentication.
11 12 13 14 15 |
# File 'lib/nopassword/session/authentication.rb', line 11 def initialize(session, **attributes) @session = session data = @session[session_key] || {} super(**data.symbolize_keys.merge(attributes)) end |
Class Method Details
.update(session) ⇒ Object
Update session data, optionally yielding for modifications
47 48 49 50 51 52 |
# File 'lib/nopassword/session/authentication.rb', line 47 def self.update(session, **) new(session, **).tap do |instance| yield instance if block_given? instance.save_without_validation end end |
Instance Method Details
#challenge ⇒ Object
22 23 24 |
# File 'lib/nopassword/session/authentication.rb', line 22 def challenge @challenge ||= build_challenge(identifier:) end |
#delete ⇒ Object
54 55 56 57 |
# File 'lib/nopassword/session/authentication.rb', line 54 def delete @session.delete(session_key) Link::Challenge.delete(@session) end |
#identifier ⇒ Object
Override in subclass to return the identifier (email, phone, etc.)
18 19 20 |
# File 'lib/nopassword/session/authentication.rb', line 18 def identifier raise NotImplementedError, "Subclasses must implement #identifier" end |
#persisted? ⇒ Boolean
59 60 61 |
# File 'lib/nopassword/session/authentication.rb', line 59 def persisted? false end |
#save ⇒ Object
30 31 32 33 34 |
# File 'lib/nopassword/session/authentication.rb', line 30 def save return false unless valid? @session[session_key] = attributes.compact true end |
#save! ⇒ Object
36 37 38 |
# File 'lib/nopassword/session/authentication.rb', line 36 def save! save || raise(ActiveModel::ValidationError.new(self)) end |
#save_without_validation ⇒ Object
Save without running validations - useful for syncing from challenge
41 42 43 44 |
# File 'lib/nopassword/session/authentication.rb', line 41 def save_without_validation @session[session_key] = attributes.compact true end |
#verification(token:) ⇒ Object
26 27 28 |
# File 'lib/nopassword/session/authentication.rb', line 26 def verification(token:) build_verification(token:) end |