-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add jwt to users and application controller
- Loading branch information
1 parent
0f9d1b1
commit f3df0eb
Showing
3 changed files
with
54 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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== |