Hi all, what is an example of a session map to pass to ::http/enable-session
?
>:enable-session: A settings map to include the session middleware interceptor. If nil, this interceptor is not added. Default is nil.
As per ring session opts: https://github.com/ring-clojure/ring/blob/master/ring-core/src/ring/middleware/session.clj#L16-L18
Perfect. That’s the default?
Can I just add true
?
Thanks
By default this map will be used here
(from ring.middleware.session
)
(defn- session-options
[options]
{:store (options :store (mem/memory-store))
:cookie-name (options :cookie-name "ring-session")
:cookie-attrs (merge {:path "/"
:http-only true}
(options :cookie-attrs)
(if-let [root (options :root)]
{:path root}))})
Those are the defaults so should be able to add :enable-session {}
to service map
so your map should contain #{:store :cookie-name :cookie-attrs :root}
Thanks works 🙂
:thumbsup:
Hmm actually the cookie is not stored
I expect a ring-session cookie, but it is not added
I use the default settings, probably miss something
(ns my-service
(:require [io.pedestal.http :as http]
[io.pedestal.http.body-params :as body-params]
[io.pedestal.http.route :as route]
[ring.util.response :as ring-resp]))
(defn start
[request]
(println "start" request)
(ring-resp/response (format "Clojure %s - served from %s"
(clojure-version)
(route/url-for ::start))))
(defn auth
[request]
(println "auth" request)
(ring-resp/response "Hello World!"))
(def common-interceptors
[(body-params/body-params) http/html-body])
(def routes
#{["/start" :get (conj common-interceptors `start)]
["/auth" :get (conj common-interceptors `auth)]})
(def service
{:env :prod
::http/routes routes
::http/enable-session {}
::http/resource-path "/public"
::http/type :jetty
::http/host "0.0.0.0"
::http/port 8080
::http/container-options {:h2c? true
:h2? false
:ssl? false}})
It does add :session/key
Always nil
And a :session
that’s also nil
No cookies in response
What else do I have to add? Or return a different type of response?
@erwinrooijakkers have you looked at https://github.com/ring-clojure/ring/wiki/Sessions?
Yes saw it. I also saw https://github.com/pedestal/pedestal/blob/master/samples/ring-middleware/src/ring_middleware/service.clj
There’s an example there so I apparently have to do more
Create an interceptor
Don’t know how it related to the service map
And ::http/enable-session {}
If you use io.pedestal.http/default-interceptors
without setting the :io.pedestal.http/interceptors
kw then it should create the session interceptor for you when ::http/enable-session
is provided
https://github.com/pedestal/pedestal/blob/master/service/src/io/pedestal/http.clj#L208-L209
https://github.com/pedestal/pedestal/blob/master/service/src/io/pedestal/http.clj#L257
Ah ok so I need default inerceptors
yep, sorry. Assumed that you were using that
Hmm how do I add those?
Oh Is ee
there are already added
At least in run-dev
Not in -main it seems like
From the pedestal-service template
they are
;; This is an adapted service map, that can be started and stopped
;; From the REPL you can call server/start and server/stop on this service
(defonce runnable-service
(server/create-server service/service))
(defn run-dev
"The entry-point for 'lein run-dev'"
[& args]
(println "\nCreating your [DEV] server...")
(-> service/service ;; start with production configuration
(merge {:env :dev
;; do not block thread that starts web server
::server/join? false
;; Routes can be a function that resolve routes,
;; we can use this to set the routes to be reloadable
::server/routes #(route/expand-routes (deref #'service/routes))
;; all origins are allowed in dev mode
::server/allowed-origins {:creds true :allowed-origins (constantly true)}
;; Content Security Policy (CSP) is mostly turned off in dev mode
::server/secure-headers {:content-security-policy-settings {:object-src "'none'"}}})
;; Wire up interceptor chains
server/default-interceptors
server/dev-interceptors
server/create-server
server/start))
(defn -main
"The entry-point for 'lein run'"
[& args]
(println "\nCreating your server...")
(server/start runnable-service))
In server.cljs
create-server
calls create-provider
which does this https://github.com/pedestal/pedestal/blob/master/service/src/io/pedestal/http.clj#L323
Okay so I have the default-interceptors thnaks
And default enable-session {}
should create cookie store
And a ring-session cookie
But that does not happen
are you putting stuff in session?
No
refer to that doc
Ah if I add something to the session, then there’s a cookie
Great
Thanks!
:thumbsup: