pedestal

erwinrooijakkers 2019-08-28T14:38:18.000900Z

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

erwinrooijakkers 2019-08-28T14:39:00.001200Z

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

erwinrooijakkers 2019-08-28T14:43:46.002Z

Perfect. That’s the default?

erwinrooijakkers 2019-08-28T14:43:53.002200Z

Can I just add true?

erwinrooijakkers 2019-08-28T14:44:41.002700Z

Thanks

souenzzo 2019-08-28T14:44:45.002900Z

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

2019-08-28T14:44:56.003300Z

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

souenzzo 2019-08-28T14:46:14.003400Z

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

erwinrooijakkers 2019-08-28T14:57:08.003700Z

Thanks works 🙂

2019-08-28T15:29:55.003900Z

:thumbsup:

erwinrooijakkers 2019-08-28T15:34:27.004100Z

Hmm actually the cookie is not stored

erwinrooijakkers 2019-08-28T15:37:09.004400Z

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

erwinrooijakkers 2019-08-28T15:37:34.004600Z

I use the default settings, probably miss something

erwinrooijakkers 2019-08-28T15:37:51.005Z

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

erwinrooijakkers 2019-08-28T15:39:07.005300Z

It does add :session/key

erwinrooijakkers 2019-08-28T15:39:11.005500Z

Always nil

erwinrooijakkers 2019-08-28T15:39:39.005800Z

And a :session that’s also nil

erwinrooijakkers 2019-08-28T15:39:41.006Z

No cookies in response

erwinrooijakkers 2019-08-28T15:40:08.006400Z

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

2019-08-28T15:42:57.006800Z

@erwinrooijakkers have you looked at https://github.com/ring-clojure/ring/wiki/Sessions?

erwinrooijakkers 2019-08-28T15:45:00.007700Z

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

erwinrooijakkers 2019-08-28T15:45:07.007900Z

Create an interceptor

erwinrooijakkers 2019-08-28T15:47:10.008700Z

Don’t know how it related to the service map

erwinrooijakkers 2019-08-28T15:47:17.009Z

And ::http/enable-session {}

2019-08-28T15:47:35.009300Z

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

erwinrooijakkers 2019-08-28T15:48:18.010100Z

Ah ok so I need default inerceptors

2019-08-28T15:48:34.010300Z

yep, sorry. Assumed that you were using that

erwinrooijakkers 2019-08-28T15:49:53.010600Z

Hmm how do I add those?

erwinrooijakkers 2019-08-28T15:50:48.010800Z

Oh Is ee

erwinrooijakkers 2019-08-28T15:50:51.011100Z

there are already added

erwinrooijakkers 2019-08-28T15:51:05.011600Z

At least in run-dev

erwinrooijakkers 2019-08-28T15:51:17.011800Z

Not in -main it seems like

erwinrooijakkers 2019-08-28T15:51:21.012300Z

From the pedestal-service template

2019-08-28T15:51:22.012400Z

they are

erwinrooijakkers 2019-08-28T15:51:37.012600Z

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

erwinrooijakkers 2019-08-28T15:51:46.012900Z

In server.cljs

2019-08-28T15:52:13.013200Z

create-server calls create-provider which does this https://github.com/pedestal/pedestal/blob/master/service/src/io/pedestal/http.clj#L323

erwinrooijakkers 2019-08-28T15:52:37.013600Z

Okay so I have the default-interceptors thnaks

erwinrooijakkers 2019-08-28T15:52:55.014100Z

And default enable-session {} should create cookie store

erwinrooijakkers 2019-08-28T15:53:01.014500Z

And a ring-session cookie

erwinrooijakkers 2019-08-28T15:53:03.014700Z

But that does not happen

2019-08-28T15:53:05.014800Z

are you putting stuff in session?

erwinrooijakkers 2019-08-28T15:53:07.015100Z

No

2019-08-28T15:53:10.015200Z

refer to that doc

erwinrooijakkers 2019-08-28T15:54:32.015500Z

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

erwinrooijakkers 2019-08-28T15:54:52.015700Z

Great

erwinrooijakkers 2019-08-28T15:54:55.015900Z

Thanks!

2019-08-28T17:11:26.016200Z

:thumbsup: