# frozen_string_literal: true module Api class AuthenticationsController # The logic for the create method of AuthenticationsController. class CreateLogic def initialize(params) @email = params[:email] @password = params[:password] @user_account = UserAccount.find_by(email: @email) end def call return unless @user_account&.authenticate(@password) @user_account.session_key = SecureRandom.hex(16) @user_account.save { token: service.call(DateTime.current + 30.minutes), refresh: service.call(DateTime.current + 3.days) } end private def service @service ||= Services::TokenGenerationService.new(service_params) end def service_params { email: @email, role: @user_account.role, session_key: @user_account.session_key } end end end end