summaryrefslogtreecommitdiff
path: root/app
diff options
context:
space:
mode:
authorHombreLaser <sebastian-440@live.com>2023-02-23 17:30:00 -0600
committerHombreLaser <sebastian-440@live.com>2023-02-23 17:30:00 -0600
commit42671b4b5f38064faba02c6e220c425f39afd87c (patch)
treea7a0ed571205f0f5ffe40ee9cf80ff8852b770b2 /app
parent38d054e178f6860a1e1b3ed160808509c920bcc1 (diff)
Añade login
Diffstat (limited to 'app')
-rw-r--r--app/controllers/api/authentications_controller.rb17
-rw-r--r--app/controllers/api/authentications_controller/create_logic.rb34
2 files changed, 49 insertions, 2 deletions
diff --git a/app/controllers/api/authentications_controller.rb b/app/controllers/api/authentications_controller.rb
index 90f04d8..f2fb993 100644
--- a/app/controllers/api/authentications_controller.rb
+++ b/app/controllers/api/authentications_controller.rb
@@ -3,7 +3,16 @@
module Api
# The controller that handles authentications.
class AuthenticationsController < ApplicationController
- def create; end
+ AUTHENTICATION_ERROR = 'Credenciales incorrectas'
+
+ def create
+ @logic = logic
+ @token = @logic.call
+
+ render json: @token && return if @token
+
+ render json: { message: AUTHENTICATION_ERROR}, status: :unauthorized
+ end
def destroy; end
@@ -14,5 +23,9 @@ module Api
def permitted_params
params.require(:user_account).permit(:email, :password)
end
+
+ def service
+ @service = Services::AuthenticationService.new(permitted_params)
+ end
end
-end \ No newline at end of file
+end
diff --git a/app/controllers/api/authentications_controller/create_logic.rb b/app/controllers/api/authentications_controller/create_logic.rb
new file mode 100644
index 0000000..173db58
--- /dev/null
+++ b/app/controllers/api/authentications_controller/create_logic.rb
@@ -0,0 +1,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