Class: RodaSessionMiddleware

Inherits:
Object
  • Object
show all
Defined in:
lib/roda/session_middleware.rb

Overview

Session middleware that can be used in any Rack application that uses Roda’s sessions plugin for encrypted and signed cookies. See Roda::RodaPlugins::Sessions for details on options.

Defined Under Namespace

Classes: SessionHash

Instance Method Summary collapse

Constructor Details

#initialize(app, opts) ⇒ RodaSessionMiddleware

Setup the middleware, passing opts as the Roda sessions plugin options.



154
155
156
157
158
159
# File 'lib/roda/session_middleware.rb', line 154

def initialize(app, opts)
  mid = Class.new(Roda)
  mid.plugin :sessions, opts
  @req_class = mid::RodaRequest
  @app = app
end

Instance Method Details

#call(env) ⇒ Object

Initialize the session hash in the environment before calling the next application, and if the session has been loaded after the result has been returned, then persist the session in the cookie.



164
165
166
167
168
169
170
171
172
173
174
# File 'lib/roda/session_middleware.rb', line 164

def call(env)
  session = env['rack.session'] = SessionHash.new(@req_class.new(nil, env))

  res = @app.call(env)

  if session.loaded?
    session.req.persist_session(res[1], session.data)
  end

  res
end