module Challah::Controller
These methods are added into ActionController::Base and are available in all of your app's controllers.
Protected Instance Methods
The user that is currently logged into this session. If there is no user logged in, nil will be returned.
@note This method is also available as a helper in your views.
@return [User, nil] The current authenticated user.
# File lib/challah/controller.rb, line 70 def current_user @current_user ||= current_user_session.user end
Is there currently a logged in user? Returns true if it is safe to use the {#current_user current_user
} method.
@note This method is also available as a helper in your views.
@see current_user
current_user
@return [Boolean] Is there a user logged in?
# File lib/challah/controller.rb, line 54 def current_user? !!current_user end
The current authentication session, if one exists. A {Session} object will be returned regardless of its valid status. If an invalid session is returned, the {Session#user user} attribute will be nil.
@return [Session] The current browser session.
# File lib/challah/controller.rb, line 79 def current_user_session @current_user_session ||= Challah::Session.find(request, params, user_model) end
Alias for current_user
?
# File lib/challah/controller.rb, line 59 def signed_in? current_user? end
Restrict a controller to only authenticated users. If someone tries to access a restricted action and is not logged in, they will be redirected to the login page.
This method is an alias for:
restrict_to_authenticated
@example
class YourController < ApplicationController before_action :login_required # ... end
@example Specifing certain actions.
class YourOtherController < ApplicationController before_action :login_required, :only => [ :create, :update, :destroy ] # ... end
@see Controller::ClassMethods#restrict_to_authenticated
restrict_to_authenticated
# File lib/challah/controller.rb, line 106 def signin_required unless signed_in? session[:return_to] = request.url redirect_to signin_path and return end end
# File lib/challah/controller.rb, line 114 def user_model @_challah_user_model ||= Challah.user end