evodata/app/controllers/api/v1/base_controller.rb

31 lines
683 B
Ruby
Raw Normal View History

class Api::V1::BaseController < ApplicationController
include ApiKeyAuthentication
# Require API key authentication
prepend_before_action :authenticate_with_api_key!
# before_action :authenticate
protect_from_forgery with: :null_session
def ping
2023-02-26 15:09:08 +01:00
render json: {message: "pong"}, status: :ok
end
private
def authenticate
authenticate_user_with_token || handle_bad_authentication
end
def authenticate_user_with_token
authenticate_with_http_token do |token, options|
@user ||= User.find_by(private_api_key: token)
end
end
def handle_bad_authentication
2023-02-26 15:09:08 +01:00
render json: {message: "Bad credentials"}, status: :unauthorized
end
2023-02-26 15:09:08 +01:00
end