Module: Clavis::Controllers::Concerns::SessionManagement
- Extended by:
- ActiveSupport::Concern
- Included in:
- AuthController
- Defined in:
- lib/clavis/controllers/concerns/session_management.rb
Overview
SessionManagement provides methods for handling user sessions after OAuth authentication This concern is designed to be included in ApplicationController and provides a simple, Rails-friendly way to handle user sessions.
Instance Method Summary collapse
-
#after_login_path ⇒ String
Default path to redirect to after successful login.
-
#after_logout_path ⇒ String
Default path to redirect to after logout.
-
#authenticated? ⇒ Boolean
Check if a user is currently authenticated.
-
#current_user ⇒ User?
Get the current authenticated user, if any.
-
#sign_in_user(user) ⇒ Object
Sign in a user by setting a signed cookie.
-
#sign_out_user ⇒ void
Sign out the current user by clearing the cookie.
-
#store_location ⇒ void
Store the current URL to return to after authentication.
Instance Method Details
#after_login_path ⇒ String
Default path to redirect to after successful login
69 70 71 |
# File 'lib/clavis/controllers/concerns/session_management.rb', line 69 def after_login_path stored_location || default_path end |
#after_logout_path ⇒ String
Default path to redirect to after logout
75 76 77 78 79 80 81 |
# File 'lib/clavis/controllers/concerns/session_management.rb', line 75 def after_logout_path if respond_to?(:login_path) login_path else default_path end end |
#authenticated? ⇒ Boolean
Check if a user is currently authenticated
20 21 22 |
# File 'lib/clavis/controllers/concerns/session_management.rb', line 20 def authenticated? current_user.present? end |
#current_user ⇒ User?
Get the current authenticated user, if any
26 27 28 29 30 |
# File 'lib/clavis/controllers/concerns/session_management.rb', line 26 def current_user return @current_user if defined?(@current_user) @current_user = end |
#sign_in_user(user) ⇒ Object
Sign in a user by setting a signed cookie
34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/clavis/controllers/concerns/session_management.rb', line 34 def sign_in_user(user) # First try to use Devise if it's available if respond_to?(:sign_in) && !method("sign_in").owner.is_a?(Method) sign_in(user) else # Use our secure cookie-based approach .signed.permanent[:user_id] = { value: user.id, httponly: true, same_site: :lax, secure: Rails.env.production? } end end |
#sign_out_user ⇒ void
This method returns an undefined value.
Sign out the current user by clearing the cookie
51 52 53 54 55 56 57 58 59 |
# File 'lib/clavis/controllers/concerns/session_management.rb', line 51 def sign_out_user # First try to use Devise if it's available if respond_to?(:sign_out) && !method("sign_out").owner.is_a?(Method) sign_out(current_user) else # Use our cookie-based approach .delete(:user_id) end end |
#store_location ⇒ void
This method returns an undefined value.
Store the current URL to return to after authentication
63 64 65 |
# File 'lib/clavis/controllers/concerns/session_management.rb', line 63 def store_location session[:return_to] = request.url if request.get? end |