Module: RrxApi::Authenticatable
- Extended by:
- ActiveSupport::Concern
- Included in:
- Controller
- Defined in:
- app/controllers/concerns/rrx_api/authenticatable.rb
Overview
Controller concern providing authentication plumbing.
Include this in your ApplicationController (it is included in RrxApi::Controller
by default). Then call authenticate! on any controller that requires auth:
class Api::V1::PostsController < ApplicationController
authenticate!
end
Override authenticated_user in your ApplicationController to resolve the
current user from the request (token, API key, etc.). The default implementation
returns nil, which means all requests are unauthenticated unless overridden.
In test environments, requests may pass an X-Test-User-Id header instead of
a real token. Override test_user if your User model has a different lookup.
Constant Summary collapse
- AUTH_HEADER =
'Authorization'- BEARER_TOKEN =
'Bearer'
Instance Method Summary collapse
-
#current_user ⇒ Object
Returns the authenticated user or
nil.
Instance Method Details
#current_user ⇒ Object
Returns the authenticated user or nil.
In test env the X-Test-User-Id header short-circuits real auth.
42 43 44 |
# File 'app/controllers/concerns/rrx_api/authenticatable.rb', line 42 def current_user @current_user ||= (Rails.env.test? ? test_user : nil) || authenticated_user end |