Created
May 14, 2024 15:37
-
-
Save fractaledmind/a595097e1796290dc23f9386e2dd3072 to your computer and use it in GitHub Desktop.
Example of minimal authentication requirements for Rails app
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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