- Crystal 1.0.0 support 🎉
- Kemal 1.0.0 support 🎉
- Secure cookies with
samesite
#96. Thanks @kingsleysh 🙏
- Crystal 0.36.0 support 🎉
- Crystal 0.34.0 support 🎉
- Crystal 0.27.2 support 🎉
- Kemal 0.25.2 support 🎉
- Crystal 0.27.0 support 🎉
- Crystal 0.25.0 support 🎉
- Crystal 0.24.1 support 🎉
This is a major release which adds StoreableObject
. Big thanks to @neovintage :+1
kemal-session
has the ability to save objects to session storage. By saving objects to session storage, this opens up the ability to have more advanced data types that aren't supported by the base types (Int32, Float64, String, Bool).
Any object that you want to save to session storage needs to be a subclass of Session::StorableObject
.
The subclass needs to define two different methods. First, a class method to deserialize the object from a String, called unserialize
. The
second method, is an instance method called serialize
. serialize
will take the object and turn it into a String for the session storage engine to
handle. Here's an example implementation:
class UserStorableObject < Session::StorableObject
property id, name
def initialize(@id : Int32, @name : String); end
def serialize
return "#{@id};#{@name}"
end
def self.unserialize(value : String)
parts = value.split(";")
return self.new(parts[0].to_i, parts[1])
end
end
Once a StorableObject
subclass has been defined, you can save that in session storage just like the base types. Here's an example using
the UserStorableObject
implementation:
require "kemal"
require "kemal-session"
get "/set" do |env|
user = UserStorableObject.new(123, "charlie")
env.session.object("user", user)
end
get "/get" do |env|
user = env.session.object("user").as(UserStorableObject)
"The user stored in session is #{user.name}"
end
Serialization is up to you. You can define how you want that to happen so long as the resulting type is a String. If you need recommendations or advice, check with the underlying session storage implementation.
This is a major release which adds Session administration capabilities (check #7 for more info). Big thanks to @neovintage and @Thyra 🎉
#get
to get a session with the givensession_id
.#all
to get every saved sessions.#each
to iterate through all sessions..destroy
and#destroy
to remove a session.#destroy_all
to remove all sessions.
- Sign cookies with
secret
. It's required to have asecret
. (thanks @neovintage) - Fix multiple GC initilization.