Class: Core::Services::Sessions

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

Overview

Service concerning sessions (log in and log out)

Author:

Instance Attribute Summary

Attributes inherited from Base

#services

Instance Method Summary collapse

Methods inherited from Base

#initialize

Constructor Details

This class inherits a constructor from Core::Services::Base

Instance Method Details

#create(username, password) ⇒ 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)

    the name of the user trying to log in

  • password (string)

    the password the user has provided

Returns:



18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/core/services/sessions.rb', line 18

def create(username, password)
   = services.accounts.get_by_username(username)
  if BCrypt::Password.new(.password_digest) != password
    raise Core::Helpers::Errors::Forbidden.new(
      field: 'password',
      error: 'wrong'
    )
  end
  return Core::Models::Authentication::Session.create(
    account: ,
    token: SecureRandom.uuid
  )
end