Fork me on GitHub
#pedestal
<
2019-08-28
>
erwinrooijakkers14:08:18

Hi all, what is an example of a session map to pass to ::http/enable-session?

souenzzo14:08:45

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}))})

souenzzo14:08:14

so your map should contain #{:store :cookie-name :cookie-attrs :root}

erwinrooijakkers14:08:00

>:enable-session: A settings map to include the session middleware interceptor. If nil, this interceptor is not added. Default is nil.

erwinrooijakkers14:08:46

Perfect. That’s the default?

erwinrooijakkers14:08:53

Can I just add true?

ddeaguiar14:08:56

Those are the defaults so should be able to add :enable-session {} to service map

erwinrooijakkers14:08:08

Thanks works 🙂

erwinrooijakkers15:08:27

Hmm actually the cookie is not stored

erwinrooijakkers15:08:09

I expect a ring-session cookie, but it is not added

erwinrooijakkers15:08:34

I use the default settings, probably miss something

erwinrooijakkers15:08:51

(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}})

erwinrooijakkers15:08:07

It does add :session/key

erwinrooijakkers15:08:39

And a :session that’s also nil

erwinrooijakkers15:08:41

No cookies in response

erwinrooijakkers15:08:08

What else do I have to add? Or return a different type of response?

erwinrooijakkers15:08:00

There’s an example there so I apparently have to do more

erwinrooijakkers15:08:07

Create an interceptor

erwinrooijakkers15:08:10

Don’t know how it related to the service map

erwinrooijakkers15:08:17

And ::http/enable-session {}

ddeaguiar15:08:35

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

erwinrooijakkers15:08:18

Ah ok so I need default inerceptors

ddeaguiar15:08:34

yep, sorry. Assumed that you were using that

erwinrooijakkers15:08:53

Hmm how do I add those?

erwinrooijakkers15:08:51

there are already added

erwinrooijakkers15:08:05

At least in run-dev

erwinrooijakkers15:08:17

Not in -main it seems like

erwinrooijakkers15:08:21

From the pedestal-service template

erwinrooijakkers15:08:37

;; 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))

erwinrooijakkers15:08:37

Okay so I have the default-interceptors thnaks

erwinrooijakkers15:08:55

And default enable-session {} should create cookie store

erwinrooijakkers15:08:01

And a ring-session cookie

erwinrooijakkers15:08:03

But that does not happen

ddeaguiar15:08:05

are you putting stuff in session?

ddeaguiar15:08:10

refer to that doc

erwinrooijakkers15:08:32

Ah if I add something to the session, then there’s a cookie