diff options
Diffstat (limited to 'app/controllers')
3 files changed, 55 insertions, 7 deletions
diff --git a/app/controllers/api/authentications_controller/create_logic.rb b/app/controllers/api/authentications_controller/create_logic.rb index c62c300..2f42852 100644 --- a/app/controllers/api/authentications_controller/create_logic.rb +++ b/app/controllers/api/authentications_controller/create_logic.rb @@ -4,6 +4,8 @@ module Api class AuthenticationsController # The logic for the create method of AuthenticationsController. class CreateLogic + include TokenGenerationConcern + def initialize(params) @email = params[:email] @password = params[:password] @@ -13,17 +15,12 @@ module Api 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) } + generate_user_session_key + generate_token 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 diff --git a/app/controllers/api/user_accounts_controller/create_logic.rb b/app/controllers/api/user_accounts_controller/create_logic.rb new file mode 100644 index 0000000..3876c00 --- /dev/null +++ b/app/controllers/api/user_accounts_controller/create_logic.rb @@ -0,0 +1,31 @@ +# frozen_string_literal: true + +module Api + class UserAccountsController + # The UserAccountsController CreateLogic class. + class CreateLogic + attr_reader :user_account + + include TokenGenerationConcern + + def initialize(params) + @params = params + end + + def call + @user_account = UserAccount.new(@params) + + return unless @user_account.save + + generate_user_session_key + generate_token + end + + private + + def service_params + { email: @user_account.email, role: @user_account.role, session_key: @user_account.session_key } + end + end + end +end diff --git a/app/controllers/concerns/token_generation_concern.rb b/app/controllers/concerns/token_generation_concern.rb new file mode 100644 index 0000000..7f43e4c --- /dev/null +++ b/app/controllers/concerns/token_generation_concern.rb @@ -0,0 +1,20 @@ +# frozen_string_literal: true + +# Common logic for all the logics that require the token generation +# service. +module TokenGenerationConcern + extend ActiveSupport::Concern + + def service + @service ||= Services::TokenGenerationService.new(service_params) + end + + def generate_user_session_key + @user_account.session_key = SecureRandom.hex(16) + @user_account.save + end + + def generate_token + { token: service.call(DateTime.current + 30.minutes), refresh: service.call(DateTime.current + 3.days) } + end +end |