This commit is contained in:
Daw-Ran Liou 2019-10-01 07:18:19 -07:00
parent 888856b5cc
commit 78a1cc144e

View file

@ -1,10 +1,13 @@
(ns reitit.ring.middleware.session
(:require
[clojure.spec.alpha :as s]
[ring.middleware.session :as session]
[ring.middleware.session.memory :as memory]))
(s/def ::spec (s/keys :opt-un [::session]))
(def ^:private store
"The in-memory session store.
"The default shared in-memory session store.
This is used when no `:session` key is provided to the middleware."
(atom {}))
@ -12,12 +15,19 @@
(def session-middleware
"Middleware for session.
Decodes the session from a request map into the `:session` value and updates the session store
based on the response's `:session` value.
Enter:
Add the `:session` key into the request map based on the `:cookies`
in the request map.
Exit:
When `:session` key presents in the response map, update the session
store with its value. Then remove `:session` from the response map.
| key | description |
| -------------|-------------|
| `:session` | `ring.middleware.session.store/SessionStore` instance. Use `ring.middleware.session.memory/MemoryStore` by default."
{:name :session
:compile (fn [{:keys [session] :or {session {:store (memory/memory-store store)}}} _]
:spec ::spec
:compile (fn [{:keys [session]
:or {session {:store (memory/memory-store store)}}} _]
{:wrap #(session/wrap-session % session)})})