Carl Zulauf
Carl Zulauf

Reputation: 39558

How to access the current Rack::Session implementation

I am working on a gem that will need to read and write to not just to the current session, but to other "linked" sessions. Right now I am hard-coding reads and writes to the underlying session store we are using in our rails and rack apps whenever I need to update a session other than the current one. But, a more generic approach would be needed to make the solution portable to other session implementations.

In Rails apps I can look at Rails.application.config.session_store to determine the session storage implementation, and instantiate a new instance to read and write to sessions beyond the current session. Generating another instance of the session store seems a bit inefficient, but it does seem to work.

However, I can't figure out any way to query the current session store through a Rack app. env['rack.session'] and env['rack.session.options'] don't provide any insight.

The best solution would be to access the already-instantiated Rack::Session Rack app in the middleware stack. I'm not sure how to query what middleware is running and which one is the current Rack::Session implementation (just check which one is a decendent of Rack::Session::Abstract::ID?).

Any tips for querying the middleware or alternative suggestions would be greatly appreciated.

Upvotes: 4

Views: 1653

Answers (1)

Batkins
Batkins

Reputation: 5706

You can access the session in rack using request.cookies["rack.session"]. Rack < 1.0 used to allow you to actually pass session variables in through a request, but it has been disabled in newer versions of rack for security purposes. I know it's kind of late but hopefully this helps you.

Upvotes: 1

Related Questions