Class: NoPassword::Session::Authentication

Inherits:
Object
  • Object
show all
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

Email::Authentication

Class Method Summary collapse

Instance Method Summary collapse

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

#challengeObject



22
23
24
# File 'lib/nopassword/session/authentication.rb', line 22

def challenge
  @challenge ||= build_challenge(identifier:)
end

#deleteObject



54
55
56
57
# File 'lib/nopassword/session/authentication.rb', line 54

def delete
  @session.delete(session_key)
  Link::Challenge.delete(@session)
end

#identifierObject

Override in subclass to return the identifier (email, phone, etc.)

Raises:

  • (NotImplementedError)


18
19
20
# File 'lib/nopassword/session/authentication.rb', line 18

def identifier
  raise NotImplementedError, "Subclasses must implement #identifier"
end

#persisted?Boolean

Returns:

  • (Boolean)


59
60
61
# File 'lib/nopassword/session/authentication.rb', line 59

def persisted?
  false
end

#saveObject



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_validationObject

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