summaryrefslogtreecommitdiff
path: root/app/controllers/api/authentications_controller/create_logic.rb
blob: 173db5877d880572412b5368b4c1885693c51dcc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# frozen_string_literal: true

module Services
  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 false if @user_account.nil? && wrong_user_password?

        { token: service.call(DateTime.now + 30.minutes), refresh: service.call(DateTime.now + 3.days) }
      end

      private

      def wrong_user_password?
        @user_account.password != @password
      end

      def service
        @service ||= Services::TokenGenerationService.new(service_params)
      end

      def service_params
        { email: @email, role: @user_account.role}
      end
    end
  end
end