Module: Machina::ControllerHelpers
- Extended by:
- ActiveSupport::Concern
- Defined in:
- lib/machina/controller_helpers.rb
Overview
Convenience methods mixed into Rails controllers for authentication, authorization, and session management.
Instance Method Summary collapse
- #authenticate! ⇒ Object
- #authorized ⇒ Object
- #logged_in? ⇒ Boolean
-
#logout! ⇒ Object
Revokes the current session both locally and in the Console.
Instance Method Details
#authenticate! ⇒ Object
22 23 24 25 26 27 28 29 30 |
# File 'lib/machina/controller_helpers.rb', line 22 def authenticate! return if logged_in? if request.format.json? render json: { error: 'unauthorized' }, status: :unauthorized else redirect_to Machina.(return_to: request.original_url), allow_other_host: true end end |
#authorized ⇒ Object
14 15 16 |
# File 'lib/machina/controller_helpers.rb', line 14 def Machina::Current. || Machina::Authorized::EMPTY end |
#logged_in? ⇒ Boolean
18 19 20 |
# File 'lib/machina/controller_helpers.rb', line 18 def logged_in? .user_id.present? end |
#logout! ⇒ Object
Revokes the current session both locally and in the Console.
Deletes the local cache entry, calls the Console to revoke server-side, and removes the session cookie. Errors from the Console call are silently swallowed so local logout always succeeds.
37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/machina/controller_helpers.rb', line 37 def logout! token = [:machina_session] || extract_bearer_token if token.present? Machina.cache.delete("machina:session:#{token}") begin Machina.identity_client.revoke_session(token) rescue StandardError # Best-effort: local logout succeeds even if Console is unreachable end end .delete(:machina_session) end |