clojure - Ring: How to modify session in middleware -


how modify session in ring middleware? want have history of accessed urls stored there , can't session store values.

sessions works correctly elsewhere @ code can return responses. assume has issue , i'm not understanding how middlewares work.

here current code

(defn wrap-history [handler]                                                                                                                                                 (fn [req]                                                                                                                                 (handler (assoc-in req [:session :history]                                                                                                                                                    (vec (concat (-> req :session :history)  [(request/request-url req)]))))))                                                                 

here app (i'm using ring-defaults includes session middleware)

(def app                                                                                                                                                             (-> all-routes                                                                                                                                                                 (wrap-history)                                                                                                                                                             (wrap-defaults (assoc-in site-defaults [:security :anti-forgery] false))))                                                                   

the answer pretty easy (probably how return response middleware instead of request) don't seem able find correct documentation this..

okay correct misunderstood basics of middleware. tried modify handler request whereas should have modified returned response.

here working version of wrap-history. checks both request , response existing session session can modified other code before middleware well.

(defn wrap-history    "middleware stores last 20 visited urls in session"   [handler]                                                                      (fn [req]                                                                                                                                                                    (let [resp (handler req)                                                                                                                                                         session (or (:session resp) (:session req))                                                                                                                                updated-session (assoc session :history                                                                                                                                                           (vec (take-last 20 (concat (:history session) [(request/request-url req)]))))]                                                                  (assoc resp :session updated-session))))                                                                

Comments

Popular posts from this blog

Hatching array of circles in AutoCAD using c# -

ios - UITEXTFIELD InputView Uipicker not working in swift -

Python Pig Latin Translator -