Skip to content

class Athena::HTTP::Session
inherits Reference #

Stores data about the client across requests, such as the logged in user or flash messages.

The session is only loaded from its AHTTP::Session::Storage::Interface once it is used, and the data of each request using it is persisted once the request is done.

Attributes are stored as JSON, so any type that can be serialized via #to_json and deserialized via .from_json can be stored. E.g. scalars, arrays, hashes, or types that include JSON::Serializable.

session.set "cart", [1, 2, 3]
session.get "cart", Array(Int32) # => [1, 2, 3]
session.get? "user_id", Int64    # => nil

session.flash_bag.add "notice", "Your changes were saved!"

Constructors#

.new(storage : AHTTP::Session::Storage::Interface, attributes : AHTTP::Session::AttributeBag = AHTTP::Session::AttributeBag.new, flashes : AHTTP::Session::FlashBag = AHTTP::Session::FlashBag.new, usage_reporter : Proc(Nil) | Nil = nil)#

Creates a new session, stored via the provided storage.

The optional usage_reporter is called each time the session is used, see #usage_index.

View source

Methods#

#all : Hash(String, String)#

Returns all attributes, with their values serialized as JSON.

View source

#bag(name : String) : AHTTP::Session::BagInterface#

Returns the bag registered with the provided name, starting the session if needed.

View source

#clear : Nil#

Removes all attributes.

View source

#empty? : Bool#

Returns true if none of the session's bags have data, otherwise false.

View source

#flash_bag : AHTTP::Session::FlashBag#

Returns the bag storing the session's flash messages.

View source

#get(name : String, type : T.class) : T forall T#

Returns the value of the attribute with the provided name, deserialized as the provided type.

Raises a KeyError if no attribute with that name exists.

View source

#get?(name : String, type : T.class) : T | Nil forall T#

Returns the value of the attribute with the provided name, deserialized as the provided type, or nil if it does not exist.

View source

#has?(name : String) : Bool#

Returns true if an attribute with the provided name exists, otherwise false.

View source

#id : String#

Returns the ID of the session, or an empty string if it does not have one yet.

Until the session is started, this is the ID sent by the client, which is only validated once the session starts.

View source

#id=(id : String) : Nil#

Sets the ID of the session to load once it is started.

View source

#invalidate(lifetime : Int32 | Nil = nil) : Bool#

Clears all session data and gives the session a new ID, removing the data of the current ID. Optionally sets the lifetime of its cookie in seconds, 0 meaning until the browser is closed.

Returns true if the session was given a new ID, otherwise false.

View source

#metadata_bag : AHTTP::Session::Storage::MetadataBag#

Returns the bag storing the session's metadata, such as when it was created.

View source

#migrate(destroy : Bool = false, lifetime : Int32 | Nil = nil) : Bool#

Gives the session a new ID while keeping its data, optionally removing the data of the current ID if destroy is true. Optionally sets the lifetime of its cookie in seconds, 0 meaning until the browser is closed.

Should be used with destroy set to true whenever the privileges of the client change, such as when logging in, to prevent session fixation. Otherwise the current ID keeps a copy of the session's data, e.g. the logged in user, which anyone knowing the current ID could then use.

Returns true if the session was given a new ID, otherwise false.

View source

#name : String#

Returns the name of the session, i.e. the name of its cookie.

View source

#name=(name : String) : Nil#

Sets the name of the session.

View source

#register_bag(bag : AHTTP::Session::BagInterface) : Nil#

Registers the provided bag, whose data is loaded and persisted along with the session.

View source

#remove(name : String) : Nil#

Removes the attribute with the provided name, if it exists.

View source

#save : Nil#

Persists the session data and closes the session.

The session is automatically saved once the request is done.

View source

#set(name : String, value : _) : Nil#

Sets the attribute with the provided name to the provided value.

View source

#start : Bool#

Starts the session, loading its data. Returns true once the session is started.

The session is automatically started once its data is accessed.

View source

#started? : Bool#

Returns true if the session is started, otherwise false.

View source

#usage_index : Int32#

Returns how many times the session was used, e.g. by accessing its bags or checking if it is empty.

Used to determine if a request used the session, e.g. to mark its response as private.

View source