Class: Core::Services::Sessions

Inherits:
Base
  • Object
show all
Includes:
Singleton
Defined in:
lib/core/services/sessions.rb

Overview

Service concerning sessions (log in and log out)

Author:

Instance Method Summary collapse

Methods inherited from Base

#bad_request_err, #forbidden_err, #require_parameters, #unknown_err

Instance Method Details

#create_from_credentials(username: nil, password: nil, **ignored) ⇒ Core::Models::Authentication::Session

Creates a new session from the given user credentials. IT will

  • check that the user exists in the database

  • check that the password matches the user encrypted password

If both steps are correctly passed, it will create and return a session object so that the user can have a login token.

Parameters:

  • username (string) (defaults to: nil)

    the name of the user trying to log in

  • password (string) (defaults to: nil)

    the password the user has provided

Returns:



22
23
24
25
26
27
28
29
30
31
32
# File 'lib/core/services/sessions.rb', line 22

def create_from_credentials(username: nil, password: nil, **ignored)
   = Core.svc.accounts.get_by_credentials(
    username: username,
    password: password
  )
  session = Core::Models::Authentication::Session.create(
    account: ,
    token: SecureRandom.uuid
  )
  Decorators::Session.new(session)
end

#get_by_id(session_id: nil, **ignored) ⇒ Core::Decorators::Session

Gets the session by its unique identifier.

Parameters:

  • session_id (String) (defaults to: nil)

    the unique identifier of the session you’re searching.

Returns:

Raises:



41
42
43
44
45
46
47
# File 'lib/core/services/sessions.rb', line 41

def get_by_id(session_id: nil, **ignored)
  require_parameters session_id: session_id
  session = Core::Models::Authentication::Session.find_by(token: session_id)
  raise unknown_err(field: 'session_id') if session.nil?

  Core::Decorators::Session.new(session)
end