-
-
Save nicedawg/5f003af093d7db2c9829b85a8a7a4bc5 to your computer and use it in GitHub Desktop.
[Beginning Rails 6] Listing 8-22. Modified app/controllers/application_controller.rb
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
class ApplicationController < ActionController::Base | |
helper_method :current_user, :logged_in? | |
def current_user | |
return unless session[:user_id] | |
@current_user ||= User.find_by(id: session[:user_id]) | |
end | |
def authenticate | |
logged_in? || access_denied | |
end | |
def logged_in? | |
current_user.present? | |
end | |
def access_denied | |
redirect_to(login_path, notice: "Please log in to continue") and return false | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment