Class: Foobara::Auth::Login

Inherits:
Command
  • Object
show all
Defined in:
foobara-auth-0.0.13/src/login.rb

Defined Under Namespace

Classes: InvalidPasswordError, NoUserIdEmailOrUsernameGivenError

Constant Summary

Constants included from TruncatedInspect

TruncatedInspect::MAX_LENGTH

Instance Attribute Summary collapse

Attributes included from CommandPatternImplementation::Concerns::Subcommands

#is_subcommand

Attributes included from CommandPatternImplementation::Concerns::Runtime

#exception, #outcome

Attributes included from CommandPatternImplementation::Concerns::Errors

#error_collection

Attributes included from CommandPatternImplementation::Concerns::Inputs

#inputs, #raw_inputs

Instance Method Summary collapse

Methods inherited from Command

install!, reset_all

Methods included from Concern

foobara_class_methods_module_for, foobara_concern?, included

Methods included from CommandPatternImplementation::Concerns::Reflection

#initialize

Methods included from CommandPatternImplementation::Concerns::DomainMappers

#domain_map, #domain_map!, #run_mapped_subcommand!

Methods included from CommandPatternImplementation::Concerns::Subcommands

#run_subcommand!, #subcommand?

Methods included from CommandPatternImplementation::Concerns::Entities

#load_entities, #load_records

Methods included from CommandPatternImplementation::Concerns::Transactions

#relevant_entity_classes

Methods included from NestedTransactionable

#auto_detect_current_transactions, #commit_transaction, #commit_transaction_if_open, #open_transaction, #opened_transactions, #relevant_entity_classes, relevant_entity_classes_for_type, #relevant_entity_classes_for_type, #rollback_transaction, #transactions, #use_transaction

Methods included from CommandPatternImplementation::Concerns::StateMachine

#state_machine

Methods included from CommandPatternImplementation::Concerns::Runtime

#halt!, #run, #run!, #run_execute, #succeed, #success?, #validate, #validate_records

Methods included from CommandPatternImplementation::Concerns::Errors

#initialize

Methods included from CommandPatternImplementation::Concerns::Inputs

#cast_and_validate_inputs, #initialize, #method_missing, #respond_to_missing?, #respond_to_missing_for_inputs?

Methods included from TruncatedInspect

#inspect, truncating

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Foobara::CommandPatternImplementation::Concerns::Inputs

Instance Attribute Details

#access_tokenObject

Returns the value of attribute access_token.



41
42
43
# File 'foobara-auth-0.0.13/src/login.rb', line 41

def access_token
  @access_token
end

#refresh_token_textObject

Returns the value of attribute refresh_token_text.



41
42
43
# File 'foobara-auth-0.0.13/src/login.rb', line 41

def refresh_token_text
  @refresh_token_text
end

#user_to_loginObject

Returns the value of attribute user_to_login.



41
42
43
# File 'foobara-auth-0.0.13/src/login.rb', line 41

def 
  @user_to_login
end

Instance Method Details

#executeObject



32
33
34
35
36
37
38
39
# File 'foobara-auth-0.0.13/src/login.rb', line 32

def execute
  
  verify_password
  generate_access_token
  generate_new_refresh_token

  tokens
end

#find_user_to_loginObject



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'foobara-auth-0.0.13/src/login.rb', line 43

def 
  if user
    self. = user
  elsif username
    self. = run_subcommand!(FindUser, username:)
  elsif email
    self. = run_subcommand!(FindUser, email:)
  elsif username_or_email
    begin
      self. = run_subcommand!(FindUser, username: username_or_email)
    rescue Halt
      # I'm a bit nervous about rescuing Halt and clearing the errors, but I'm more nervous bout
      # introducing a #run_subcommand method.
      if error_collection.size == 1 && error_collection.errors.first.is_a?(FindUser::UserNotFoundError) &&
         username_or_email.include?("@")
        error_collection.clear
        self. = run_subcommand!(FindUser, email: username_or_email)
      else
        raise
      end
    end
  else
    add_runtime_error(NoUserIdEmailOrUsernameGivenError)
  end
end

#generate_access_tokenObject



75
76
77
# File 'foobara-auth-0.0.13/src/login.rb', line 75

def generate_access_token
  self.access_token = run_subcommand!(BuildAccessToken, user: )
end

#generate_new_refresh_tokenObject



79
80
81
# File 'foobara-auth-0.0.13/src/login.rb', line 79

def generate_new_refresh_token
  self.refresh_token_text = run_subcommand!(CreateRefreshToken, user: )
end

#tokensObject



83
84
85
86
87
88
# File 'foobara-auth-0.0.13/src/login.rb', line 83

def tokens
  {
    access_token:,
    refresh_token: refresh_token_text
  }
end

#verify_passwordObject



69
70
71
72
73
# File 'foobara-auth-0.0.13/src/login.rb', line 69

def verify_password
  unless run_subcommand!(VerifyPassword, user: , plaintext_password:)
    add_runtime_error(InvalidPasswordError)
  end
end