Make session accept more types

This commit is contained in:
Sdogruyol 2016-09-29 21:42:46 +03:00 committed by Serdar Dogruyol
parent cf2144636d
commit 6d4bf575cb
2 changed files with 7 additions and 4 deletions

View file

@ -47,7 +47,7 @@ describe "Kemal::Middleware::CSRF" do
client_response = HTTP::Client::Response.from_io(io, decompress: false)
client_response.status_code.should eq 403
current_token = context.session["csrf"]
current_token = context.session["csrf"].as(String)
handler = Kemal::Middleware::CSRF.new
request = HTTP::Request.new("POST", "/",

View file

@ -17,6 +17,9 @@ module Kemal
class Sessions
NAME = "SessionId"
# Session Types are String, Integer, Float and Boolean
alias SessionTypes = String | Int64 | Float64 | Bool
# I hate websites which require daily login so the default
# inactivity timeout is 48 hours.
TTL = 48.hours
@ -38,7 +41,7 @@ module Kemal
def initialize(@id)
@last_access_at = Time.new.epoch_ms
@store = Hash(String, String).new
@store = Hash(String, SessionTypes).new
end
def [](key : String)
@ -51,7 +54,7 @@ module Kemal
@store[key]?
end
def []=(key : String, value : String)
def []=(key : String, value : SessionTypes)
@last_access_at = Time.now.epoch_ms
@store[key] = value
end
@ -77,7 +80,7 @@ module Kemal
@id = id
end
def []=(key : String, value : String)
def []=(key : String, value : SessionTypes)
store = STORE[id]? || begin
STORE[id] = Session.new(id)
end