mirror of
https://github.com/scinote-eln/scinote-web.git
synced 2025-04-22 20:20:50 +08:00
Improve error logging [GIOT-31]
This commit is contained in:
parent
ccf60fba7d
commit
a927c2832d
1 changed files with 14 additions and 9 deletions
|
@ -8,11 +8,13 @@ module Api
|
||||||
before_action :load_iss, except: %i(authenticate status)
|
before_action :load_iss, except: %i(authenticate status)
|
||||||
before_action :authenticate_request!, except: %i(authenticate status)
|
before_action :authenticate_request!, except: %i(authenticate status)
|
||||||
|
|
||||||
rescue_from StandardError do
|
rescue_from StandardError do |e|
|
||||||
|
logger.error e.message
|
||||||
render json: {}, status: :bad_request
|
render json: {}, status: :bad_request
|
||||||
end
|
end
|
||||||
|
|
||||||
rescue_from JWT::InvalidPayload, JWT::DecodeError do
|
rescue_from JWT::InvalidPayload, JWT::DecodeError do |e|
|
||||||
|
logger.error e.message
|
||||||
render json: { message: I18n.t('api.core.invalid_token') },
|
render json: { message: I18n.t('api.core.invalid_token') },
|
||||||
status: :unauthorized
|
status: :unauthorized
|
||||||
end
|
end
|
||||||
|
@ -35,22 +37,25 @@ module Api
|
||||||
def authenticate
|
def authenticate
|
||||||
if auth_params[:grant_type] == 'password'
|
if auth_params[:grant_type] == 'password'
|
||||||
user = User.find_by_email(auth_params[:email])
|
user = User.find_by_email(auth_params[:email])
|
||||||
raise StandardError unless user &&
|
unless user && user.valid_password?(auth_params[:password])
|
||||||
user.valid_password?(auth_params[:password])
|
raise StandardError, 'Wrong user password'
|
||||||
|
end
|
||||||
payload = { user_id: user.id }
|
payload = { user_id: user.id }
|
||||||
token = CoreJwt.encode(payload)
|
token = CoreJwt.encode(payload)
|
||||||
render json: { token_type: 'bearer', access_token: token }
|
render json: { token_type: 'bearer', access_token: token }
|
||||||
else
|
else
|
||||||
raise StandardError
|
raise StandardError, 'Wrong grant type in request'
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def load_token
|
def load_token
|
||||||
@token =
|
if request.headers['Authorization']
|
||||||
request.headers['Authorization'].scan(/Bearer (.*)$/).flatten.last
|
@token =
|
||||||
raise StandardError unless @token
|
request.headers['Authorization'].scan(/Bearer (.*)$/).flatten.last
|
||||||
|
end
|
||||||
|
raise StandardError, 'No token in the header' unless @token
|
||||||
end
|
end
|
||||||
|
|
||||||
def authenticate_request!
|
def authenticate_request!
|
||||||
|
@ -75,7 +80,7 @@ module Api
|
||||||
|
|
||||||
def load_iss
|
def load_iss
|
||||||
@iss = CoreJwt.read_iss(token)
|
@iss = CoreJwt.read_iss(token)
|
||||||
raise JWT::InvalidPayload unless @iss
|
raise JWT::InvalidPayload, 'Wrong ISS in the token' unless @iss
|
||||||
end
|
end
|
||||||
|
|
||||||
def auth_params
|
def auth_params
|
||||||
|
|
Loading…
Add table
Reference in a new issue