You cannot use the Sinatra session method in the Rack middleware, but you can access the session directly through the env hash.
Make sure the session middleware is in front of your middleware (so in Sinatra enable :sessions should be before use MyMiddleware ), then the session is accessible using the 'rack.session' key:
class MyMiddleware def initialize(app, options={}) @app = app end def call(env) puts env['rack.session'].inspect @app.call(env) end end
You might want to use the Rack::Request object to simplify access to the session and other parts of the env hash:
def call(env) request = Rack::Request.new(env) puts request.session.inspect
source share