dawarich/app/controllers/api_controller.rb

51 lines
1.1 KiB
Ruby
Raw Normal View History

2024-08-25 14:19:02 -04:00
# frozen_string_literal: true
class ApiController < ApplicationController
skip_before_action :verify_authenticity_token
before_action :authenticate_api_key
private
def authenticate_api_key
return head :unauthorized unless current_api_user
true
end
def authenticate_active_api_user!
render json: { error: 'User is not active' }, status: :unauthorized unless current_api_user&.active_until&.future?
true
end
2024-08-25 14:19:02 -04:00
def current_api_user
@current_api_user ||= User.find_by(api_key:)
end
def api_key
params[:api_key] || request.headers['Authorization']&.split(' ')&.last
2024-08-25 14:19:02 -04:00
end
def validate_params
missing_params = required_params.select { |param| params[param].blank? }
if missing_params.any?
render json: {
error: "Missing required parameters: #{missing_params.join(', ')}"
}, status: :bad_request and return
end
params.permit(*required_params)
end
def required_params
[]
end
2025-05-16 12:51:48 -04:00
def validate_points_limit
limit_exceeded = PointsLimitExceeded.new(current_api_user).call
render json: { error: 'Points limit exceeded' }, status: :unauthorized if limit_exceeded
end
2024-08-25 14:19:02 -04:00
end