Fork me on GitHub
#ring
<
2021-10-14
>
Santiago14:10:09

I would like to store some information about a logged in user in the session. I tried doing it via a middleware, but every time I visit a new page/route, the middleware that collects user info runs again because the info from the previous session is gone. Do I need to explicitly assoc response :session session in all of my routes to keep these values around from page to page?

(defn wrap-user-info
  [handler]
  (fn [request]
    (if (contains? (:session request) :okta-user-info)
      (handler request)
      (let [okta-user-info-res (okta-user-info request)]
        (info "Adding user info to session")
        (handler (-> request
                     (assoc-in [:session :okta-user-info] okta-user-info-res)))))))

seancorfield16:10:47

If you have middleware in your stack to handle sessions, it should "just work" I think.

seancorfield16:10:06

I do recall running into some issues at one point because I had middleware in the wrong order and sessions were being lost if my handler used response to generate the result. That's why ring-defaults is such a useful library: it makes sure you have (almost) everything in place in the right order.