Authlogic in recent versions require a controller object to be instantiated otherwise it can't find the session data necessary for loading a session.
In fact, contained under authlogic/controller_adapters/
are adapters for various controller implementations: Rails Controllers, Sinatra and Rack.
The interface is fairly simple, so I wrote an adapter to work with the ActionDispatch::Request
that one has access to within a constraint.
This has been tested on Rails 4.2, Rails 5 should be similar.
class AuthlogicRequestAdapter
attr_reader :request
def initialize(request)
@request = request
end
def authenticate_with_http_basic
end
def cookies
request.cookies
end
def params
request.params
end
def session
_, session_hash = Rails.application.config.session_store.new(Rails.application, Rails.application.config.session_options).load_session(request.env)
session_hash
end
end
Example of use in Rails
Route Constraint
class UserRouteConstraint
def matches?(request)
Authlogic::Session::Base.controller = AuthlogicRequestAdapter.new(request)
user = UserSession.find
if user&.record.present?
true
else
false
end
end
end
routes.rb
MyRailsApp::Application.routes.draw do
# ... other routes
constraints(UserRouteContraint.new) do
root :to => 'users#index'
end
root :to => 'welcome#index'
get '/' => 'users#index', :as => 'user_root'
end