Class: Mack::SessionStore::Cookie

Inherits:
Base show all
Defined in:
lib/mack/sessions/cookie_session_store.rb

Overview

Stores session information in the user’s cookie. The session information is encrypted using the mack-encryption library. This is the default session store for Mack applications. To set the expiry time for this session store use the following configatron setting:

cookie_session_store::expiry_time: <%= 4.hours %>

It is recommend that you set the configatron setting ‘default_secret_key’ to something, otherwise it will generate a random one each time you start your application, which could make decrypting cookies a bit of a pain. :)

Class Method Summary collapse

Methods inherited from Base

expire_all

Class Method Details

.expire(id, request, response, cookies) ⇒ Object

Deletes the cookie.



36
37
38
39
# File 'lib/mack/sessions/cookie_session_store.rb', line 36

def expire(id, request, response, cookies)
  cookies.delete(id)
  response.delete_cookie(id)
end

.get(id, request, response, cookies) ⇒ Object

Returns a decrypted session from the cookie or nil.



17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/mack/sessions/cookie_session_store.rb', line 17

def get(id, request, response, cookies)
  c = cookies[id]
  return nil if c.nil?
  begin
    sess = YAML.load(c.decrypt)
    return sess
  rescue Exception => e
    # The cookie was bad, delete it and start a new session.
    expire(id, request, response, cookies)
    return nil
  end
end

.set(id, request, response, cookies) ⇒ Object

Encrypts the session and places it into the cookie.



31
32
33
# File 'lib/mack/sessions/cookie_session_store.rb', line 31

def set(id, request, response, cookies)
  cookies[id] = {:value => YAML.dump(request.session).encrypt, :expires => (Time.now + configatron.mack.cookie_session_store.expiry_time)}
end