summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHombreLaser <sebastian-440@live.com>2023-02-26 20:19:22 -0600
committerHombreLaser <sebastian-440@live.com>2023-02-26 20:19:22 -0600
commitc86a5c9e05b183f10a63fb693c8af1d1d5a52e97 (patch)
treeb8dd53b21b7e6ef9450ceeea3c61208a32052c64
parent64171389726e6fde709f3f567eb27c5c94496b81 (diff)
Refactoriza los create logics
-rw-r--r--app/controllers/api/authentications_controller/create_logic.rb8
-rw-r--r--app/controllers/api/user_accounts_controller/create_logic.rb8
-rw-r--r--app/controllers/application_controller.rb19
-rw-r--r--app/controllers/authenticated_controller.rb17
4 files changed, 21 insertions, 31 deletions
diff --git a/app/controllers/api/authentications_controller/create_logic.rb b/app/controllers/api/authentications_controller/create_logic.rb
index 2f42852..782c760 100644
--- a/app/controllers/api/authentications_controller/create_logic.rb
+++ b/app/controllers/api/authentications_controller/create_logic.rb
@@ -15,15 +15,9 @@ module Api
def call
return unless @user_account&.authenticate(@password)
- generate_user_session_key
+ generate_user_session_key(@user_account)
generate_token
end
-
- private
-
- def service_params
- { email: @email, role: @user_account.role, session_key: @user_account.session_key }
- end
end
end
end
diff --git a/app/controllers/api/user_accounts_controller/create_logic.rb b/app/controllers/api/user_accounts_controller/create_logic.rb
index 3876c00..2e686b3 100644
--- a/app/controllers/api/user_accounts_controller/create_logic.rb
+++ b/app/controllers/api/user_accounts_controller/create_logic.rb
@@ -17,15 +17,9 @@ module Api
return unless @user_account.save
- generate_user_session_key
+ generate_user_session_key(@user_account)
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/application_controller.rb b/app/controllers/application_controller.rb
index 857414a..ea108b4 100644
--- a/app/controllers/application_controller.rb
+++ b/app/controllers/application_controller.rb
@@ -5,4 +5,23 @@ class ApplicationController < ActionController::API
def logic(permitted_params)
@logic = Object.const_get("#{self.class}::#{action_name.camelize}Logic").new(permitted_params)
end
+
+ protected
+
+ def current_user_account
+ return if decoded_token.nil?
+
+ email = decoded_token[0]['data']
+ @current_user_account ||= UserAccount.find_by(email:)
+ end
+
+ def authentication_token
+ @authentication_token ||= request.headers[:authorization]&.sub(/^Bearer /, '')
+ end
+
+ def decoded_token
+ @decoded_token ||= JWT.decode(authentication_token, ENV['HMAC_SECRET_KEY'], true, { algorithm: 'HS512' })
+ rescue JWT::ExpiredSignature
+ @decoded_token = nil
+ end
end
diff --git a/app/controllers/authenticated_controller.rb b/app/controllers/authenticated_controller.rb
index 2602064..56be1ad 100644
--- a/app/controllers/authenticated_controller.rb
+++ b/app/controllers/authenticated_controller.rb
@@ -12,23 +12,6 @@ class AuthenticatedController < ApplicationController
render json: { error_message: 'Token inválido' }, status: :unauthorized
end
- def decoded_token
- @decoded_token ||= JWT.decode(authentication_token, ENV['HMAC_SECRET_KEY'], true, { algorithm: 'HS512' })
- rescue JWT::ExpiredSignature
- @decoded_token = nil
- end
-
- def current_user_account
- return if decoded_token.nil?
-
- email = decoded_token[0]['data']
- @current_user_account ||= UserAccount.find_by(email:)
- end
-
- def authentication_token
- @authentication_token ||= request.headers[:authorization]&.sub(/^Bearer /, '')
- end
-
def valid_token
!(decoded_token.nil? || current_user_account&.session_key.nil? || invalid_jti)
end