Skip to content

Commit

Permalink
feat: add jwt to users and application controller
Browse files Browse the repository at this point in the history
  • Loading branch information
claunicole committed Dec 3, 2024
1 parent 0f9d1b1 commit f3df0eb
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 3 deletions.
24 changes: 24 additions & 0 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,26 @@
class ApplicationController < ActionController::API
before_action :authenticate_request

attr_reader :current_user

private

def authenticate_request
header = request.headers['Authorization']
if header.present?
token = header.split(' ').last
begin
decoded = JsonWebToken.decode(token)
@current_user = User.find(decoded['user_id'])
rescue ActiveRecord::RecordNotFound => e
render json: { errors: 'Usuario no encontrado' }, status: :unauthorized
rescue JWT::ExpiredSignature
render json: { errors: 'Token expirado' }, status: :unauthorized
rescue JWT::DecodeError
render json: { errors: 'Token inválido' }, status: :unauthorized
end
else
render json: { errors: 'Token no proporcionado' }, status: :unauthorized
end
end
end
31 changes: 29 additions & 2 deletions app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,28 @@
class UsersController < ApplicationController
skip_before_action :authenticate_request, only: [:register, :login]


def register
user = User.new(user_params)
user.pin = params[:pin]

if user.save
account = user.accounts.first
render json: { user: user.first_name, email:user.email, cuenta: account.account_number, message: 'Usuario registrado exitosamente' }, status: :created
token = JsonWebToken.encode(user_id: user.id)
render json: {
message: 'Usuario registrado exitosamente',
token: token,
user: {
id: user.id,
first_name: user.first_name,
last_name: user.last_name,
email: user.email
},
account: {
account_number: account.account_number,
balance: account.balance
}
}, status: :created
else
render json: { errors: user.errors.full_messages }, status: :unprocessable_entity
end
Expand All @@ -16,7 +32,18 @@ def login
account = Account.find_by(account_number: params[:account_number])

if account && account.user.authenticate_pin(params[:pin])
render json: { message: 'Inicio de sesión exitoso' }, status: :ok
user = account.user
token = JsonWebToken.encode(user_id: user.id)
render json: {
message: 'Inicio de sesión exitoso',
token: token,
user: {
id: user.id,
first_name: user.first_name,
last_name: user.last_name,
email: user.email
}
}, status: :ok
else
render json: { errors: 'Número de cuenta o PIN incorrecto' }, status: :unauthorized
end
Expand Down
2 changes: 1 addition & 1 deletion config/credentials.yml.enc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
NlH3tXHQyPCNpzbS6K89UnF3NIg8+5/A5gTo90+9c8Z6J+q2HnjYkI226lvwBYGCbr/mNVZVvbYV/MCa5KgRWsuJRU5HsJJVi0MsFmx7x6tQDM7J7abDPFhu+ndLd+V34F4MM7zAuRODFi53jfhWcZJNy6Cs8rEDcQnfWAyBjkCOZsJMdLpKDLcBKu0O6O3qioEQaSYRnd2Ox6j+E/T4PfTne0YguVKGZNjqp1pzwFWDJm3E4wcATEffrum5TxjgO6d+DqB1ivPj603QekKWHNsU+AtMtmulnEK6ScvKK72MUb45bsnKxnomocugmK/xjFMZr4v0geD7JbA7OXSsQQJSo2Q+DQu7Pso5ltAg19jMKtMa6h+1SwsMLG2AwTzblnngNFwTM+4QCKTebnBZBYz2ZAaD--5+5ZzEGjtyRZ37vj--36slWOcj4BJA+78AKdqaiQ==
MmQq+RJr29zPL0e/J79TXasTbBJLsSgiCiy629HSI7vF46iNlvX4WnGqCwhJUE1328R6E1BwJRBuJ4osUg7ixN3D8dFbBUU8UcSM452XJIpMQWJhsJeL753Bb1pBzIiUCPG7VAllryAHcCfmv+gKuqBnaTXprGEUmnhsYp+lRyQfOdIk1iNOttfT70BGSeAIsoKKEMDNQCQ+8sXxyPjoCJDM7rmOdhyeFZaNlZk5ro7bqUO4i1pUN9HdWFoDZ9VgmiJhYhsvdnwtwd8Mbzceq5gSXXq1DLT+4N7JuBOSV3DncPeXAnbl4xBaaFHskgIkzN0z5hTdwIWOB9ESdHdJt4/m8Q49pUO4RGppna03rCRBX5qIrsQ8bqFOb+Z+qh01PsA9IvrRVB9c6meqLanckk7N3QgW--8IreM445Cy7l2+fa--GNuF2FfzMDviSvNZQN8hYQ==

0 comments on commit f3df0eb

Please sign in to comment.