You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
33 lines
1005 B
33 lines
1005 B
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!
|
|
token = request.authorization.to_s.delete_prefix("Bearer ").strip
|
|
digest = Digest::SHA256.hexdigest(token) if token.present?
|
|
@current_user = User.find_by(api_token_digest: digest)
|
|
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
|