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 = HTTP::Client::Response.from_io(io, decompress: false)
client_response.status_code.should eq 403 client_response.status_code.should eq 403
current_token = context.session["csrf"] current_token = context.session["csrf"].as(String)
handler = Kemal::Middleware::CSRF.new handler = Kemal::Middleware::CSRF.new
request = HTTP::Request.new("POST", "/", request = HTTP::Request.new("POST", "/",

View file

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