Class: SolidLog::UI::BaseController

Inherits:
Object
  • Object
show all
Includes:
Turbo::Streams::TurboStreamsTagBuilder
Defined in:
app/controllers/solid_log/ui/base_controller.rb

Instance Method Summary collapse

Instance Method Details

#authenticate_user! ⇒ Object

Override this method in your host application to implement custom authentication

Example configurations in config/initializers/solid_log_ui.rb:

  1. Using a Proc/Lambda:
SolidLog::UI.configure do |config|
config.authentication_method = ->(controller) {
  controller.redirect_to controller.root_path unless controller.current_user&.admin?
}
end
  1. Using a method name:
SolidLog::UI.configure do |config|
config.authentication_method = :require_admin
end

class ApplicationController
def require_admin
  redirect_to root_path unless current_user&.admin?
end
end
  1. Using basic auth:
SolidLog::UI.configure do |config|
config.authentication_method = :basic
end


54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'app/controllers/solid_log/ui/base_controller.rb', line 54

def authenticate_user!
  config = SolidLog::UI.configuration
  auth_method = config.authentication_method

  case auth_method
  when :none
    # No authentication required
    true
  when :basic
    authenticate_or_request_with_http_basic("SolidLog") do |username, password|
      authenticate_with_basic_auth(username, password)
    end
  when Proc
    # Call the proc in the controller's context
    instance_exec(&auth_method)
  when Symbol
    # Call the named method on the controller
    if respond_to?(auth_method, true)
      send(auth_method)
    else
      raise NoMethodError, "Authentication method '#{auth_method}' not defined. Define it in your ApplicationController or BaseController."
    end
  else
    render plain: "Invalid authentication configuration", status: :unauthorized
    false
  end
end