Skip to content

Instantly share code, notes, and snippets.

@fractaledmind
Created May 14, 2024 15:37
Show Gist options
  • Save fractaledmind/a595097e1796290dc23f9386e2dd3072 to your computer and use it in GitHub Desktop.
Save fractaledmind/a595097e1796290dc23f9386e2dd3072 to your computer and use it in GitHub Desktop.
Example of minimal authentication requirements for Rails app
ass ApplicationController < ActionController::Base
before_action :authenticate!
helper_method :current_user
helper_method :user_signed_in?
private
def authenticate
@current_user = nil
authenticate_with_http_digest(Rails.application.credentials.auth_realm) do |username|
@current_user = User.find_by(username: username)
@current_user&.password_digest || false
end
!@current_user.nil?
end
def authenticate!
authenticate || request_http_digest_authentication(Rails.application.credentials.auth_realm)
end
def current_user
Current.user ||= @current_user
end
def user_signed_in?
Current.user.present?
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment