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

29 lines
660 B
Ruby
Raw Normal View History

class Api::V1::ApiKeysController < Api::V1::BaseController
2023-02-26 15:09:08 +01:00
# Require API key authentication
prepend_before_action :authenticate_with_api_key!, only: %i[index destroy]
2023-02-26 15:09:08 +01:00
def index
render json: current_bearer.api_keys
end
2023-02-26 15:09:08 +01:00
def create
authenticate_with_http_basic do |email, password|
user = User.find_by email: email
2023-02-26 15:09:08 +01:00
if user&.authenticate(password)
api_key = user.api_keys.create! token: SecureRandom.hex
2023-02-26 15:09:08 +01:00
render json: api_key, status: :created and return
end
end
2023-02-26 15:09:08 +01:00
render status: :unauthorized
end
2023-02-26 15:09:08 +01:00
def destroy
api_key = current_bearer.api_keys.find(params[:id])
api_key.destroy
end
end