r2web-ru/backend/app/controllers/application_controller.rb

15 lines
507 B
Ruby
Raw Normal View History

2022-11-30 00:37:19 +10:00
class ApplicationController < ActionController::API
def authorize_request
header = request.headers['Authorization']
header = header.split(' ').last if header
begin
@decoded = JsonWebToken.decode(header)
@current_user = User.find(@decoded[:user_id])
rescue ActiveRecord::RecordNotFound => e
render json: { errors: e.message }, status: :unauthorized
rescue JWT::DecodeError => e
render json: { errors: e.message }, status: :unauthorized
end
end
end