diff options
Diffstat (limited to 'app/controllers/api')
5 files changed, 24 insertions, 57 deletions
diff --git a/app/controllers/api/authentications_controller.rb b/app/controllers/api/authentications_controller.rb index 3eb52c7..b9a6f08 100644 --- a/app/controllers/api/authentications_controller.rb +++ b/app/controllers/api/authentications_controller.rb @@ -4,20 +4,29 @@ module Api # The controller that handles authentications. class AuthenticationsController < ApplicationController def create - @token = logic(permitted_params).call + @current_user_account = UserAccount.find_by(email: permitted_params[:email]) - render json: @token, status: :ok and return if @token + unless @current_user_account&.authenticate(permitted_params[:password]) + render json: { error_message: 'Credenciales incorrectas' }, status: :unauthorized and return + end - render json: { error_message: 'Credenciales incorrectas' }, status: :unauthorized + render json: generate_token, status: :ok end def destroy current_user_account.session_key = nil current_user_account.save + + render status: :no_content end private + def service_params + { email: @current_user_account.email, + role: @current_user_account.role } + end + def permitted_params params.require(:credentials).permit(:email, :password) end diff --git a/app/controllers/api/authentications_controller/create_logic.rb b/app/controllers/api/authentications_controller/create_logic.rb deleted file mode 100644 index 782c760..0000000 --- a/app/controllers/api/authentications_controller/create_logic.rb +++ /dev/null @@ -1,23 +0,0 @@ -# frozen_string_literal: true - -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] - @user_account = UserAccount.find_by(email: @email) - end - - def call - return unless @user_account&.authenticate(@password) - - generate_user_session_key(@user_account) - generate_token - end - end - end -end diff --git a/app/controllers/api/refresh_tokens_controller.rb b/app/controllers/api/refresh_tokens_controller.rb index 3b0843e..fd16079 100644 --- a/app/controllers/api/refresh_tokens_controller.rb +++ b/app/controllers/api/refresh_tokens_controller.rb @@ -3,16 +3,17 @@ module Api # The controller to generate new tokens. class RefreshTokensController < AuthenticatedController + before_action :validate_jwt + def create @current_user_account.update_attribute(:session_key, SecureRandom.hex(16)) - @token = { token: service.call(DateTime.current + 30), refresh: service.call(authentication_token[0]['exp']) } + @token = service.call(authentication_token[0]['exp']) render json: @token, status: :ok end def service_params - { email: @current_user_account.email, role: @current_user_account.role, - session_key: @current_user_account.session_key } + { email: @current_user_account.email, role: @current_user_account.role } end def service diff --git a/app/controllers/api/user_accounts_controller.rb b/app/controllers/api/user_accounts_controller.rb index 3e03cf9..ca5d93b 100644 --- a/app/controllers/api/user_accounts_controller.rb +++ b/app/controllers/api/user_accounts_controller.rb @@ -10,15 +10,20 @@ module Api end def create - @token = logic(permitted_params).call + @user_account = UserAccount.new(permitted_params) - render json: @token, status: :ok and return if @token + render json: generate_token, status: :ok and return if @user_account.save - render json: { errors: @logic.user_account.errors.full_messages }, status: :unprocessable_entity + render json: @user_account.errors.full_messages, status: :unprocessable_entity end private + def service_params + { email: @user_account.email, + role: @user_account.role } + end + def permitted_params params.require(:user_account).permit(:role, :email, :first_name, :last_name, :password) end diff --git a/app/controllers/api/user_accounts_controller/create_logic.rb b/app/controllers/api/user_accounts_controller/create_logic.rb deleted file mode 100644 index 2e686b3..0000000 --- a/app/controllers/api/user_accounts_controller/create_logic.rb +++ /dev/null @@ -1,25 +0,0 @@ -# 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(@user_account) - generate_token - end - end - end -end |