Add Avo behind Basic Authentication
Because in Rails we commonly do that using a static function on the controller we need to safely extend the controller to contain that function.
In actuality we will end up with something that behaves like this:
ruby
class Avo::ApplicationController < ::ActionController::Base
http_basic_authenticate_with name: "adrian", password: "password"
# More methods here
end
Safely add it to Avo
We described the process in depth in this article so let's get down to business.
- Add the
BasicAuth
concern - The concern will prepend the basic auth method
include
that concern to Avo'sApplicationController
WARNING
Ensure you restart the server after you extend the controller in this way.
ruby
# app/controllers/concerns/basic_auth.rb
module BasicAuth
extend ActiveSupport::Concern
# Authentication strategy came from this article:
# https://dev.to/kevinluo201/setup-a-basic-authentication-in-rails-with-http-authentication-388e
included do
http_basic_authenticate_with name: "adrian", password: "password"
end
end
# config/initializers/avo.rb
Avo.configure do |config|
# Avo configuration
end
# Add this to include it in Avo's ApplicationController
Rails.configuration.to_prepare do
# Add basic authentication to Avo
Avo::ApplicationController.include BasicAuth
end