module Api module V1 class BaseController < ApplicationController before_action :authenticate_api_user! after_action :expose_build_version rescue_from ActiveRecord::RecordNotFound, with: :not_found private attr_reader :current_user def authenticate_api_user! scheme, value = request.authorization.to_s.split(" ", 2) token = value.to_s.strip if scheme&.casecmp("Bearer")&.zero? digest = Digest::SHA256.hexdigest(token) if token.present? @current_user = User.find_by(api_token_digest: digest) if digest.present? render json: { error: "Nicht angemeldet" }, status: :unauthorized unless @current_user end def not_found render json: { error: "Nicht gefunden" }, status: :not_found end def render_validation(record) render json: { errors: record.errors.full_messages }, status: :unprocessable_content end def expose_build_version response.set_header("X-Praktikum-Build", Rails.application.config.x.build_version) end end end end