Howto use lib-noir stateful-sessions in Compojure
Asked Answered
B

3

6

I think I have a fairly straightforward problem here. But I've been looking at this screen too long. So I'm trying (and failing) to get stateful sessions working in Compojure. The refheap code paste is here.

You can see me trying to use lib-noir (line 62) to initialize stateful sessions. Then when the app is running, I try to make a call to session/put! some data in the session (line 43).

Now, this stacktrace says that in session.put!, lib-noir is trying to swap out a session var that hasn't been bound. Now, I thought I did that on line 62. So I'm sure this is a simple fix that another set of eyes will see.

java.lang.ClassCastException: clojure.lang.Var$Unbound cannot be cast to clojure.lang.Atom                                                                                                                                                                                                                                                                                                                                
        at clojure.core$swap_BANG_.invoke(core.clj:2110)                                                                                                                                                                                                                                                                                                                                                                  
        at noir.session$put_BANG_.invoke(session.clj:18)                                                                                                                                                                                                                                                                                                                                                                  
        at bkell.http.handler$fn__6159.invoke(handler.clj:156)                                                                                                                                                                                                                                                                                                                                                            
        at compojure.core$make_route$fn__3800.invoke(core.clj:93)                                                                                                                                                                                                                                                                                                                                                         
        at compojure.core$if_route$fn__3784.invoke(core.clj:39)                                                                                                                                                                                                                                                                                                                                                           
        at compojure.core$if_method$fn__3777.invoke(core.clj:24)                                                                                                                                                                                                                                                                                                                                                          
        at compojure.core$routing$fn__3806.invoke(core.clj:106)                                                                                                                                                                                                                                                                                                                                                           
        at clojure.core$some.invoke(core.clj:2390)                                                                                                                                                                                                                                                                                                                                                                        
        at compojure.core$routing.doInvoke(core.clj:106)                                                                                                                                                                                                                                                                                                                                                                  
        at clojure.lang.RestFn.applyTo(RestFn.java:139)                                                                                                                                                                                                                                                                                                                                                                   
        ...
        at java.lang.Thread.run(Thread.java:619)

Thanks

Birchfield answered 11/2, 2013 at 4:51 Comment(1)
Did you ever find a solution to this problem? I've been facing the same issue and started a thread, but didn't get any responses either.Fernand
E
2

Answered by James Reeves at - https://groups.google.com/forum/#!topic/compojure/yG6nCXiEinU

Try swapping around handler/site and wrap-noir-session*. Because handler/site applies the wrap-session middleware, and wrap-noir-session* depends on that according to the documentation, handler/site should be applied before wrap-noir-session.

  • James
Ending answered 9/11, 2013 at 6:4 Comment(0)
D
0

I think you want wrap-noir-session, not wrap-noir-session*. The documentation on wrap-noir-session* says (my emphasis) "A stateful layer around wrap-session. Expects that wrap-session has already been used."

If you want to use wrap-noir-session* I think you'll need to use wrap-session explicitly.

Dynamism answered 12/2, 2013 at 3:14 Comment(1)
Thanks for responding. Yeah, I definitely tried both with and without the asterix. I even tried removing the call to friend/authenticate. So the app def looks like: (def app (handler/site (session/wrap-noir-session* app-routes)). But I still get the same Var$Unbound error. So basically, some session atom is not getting bound when I call wrap-noir-session. So I'm looking for the right incantation.Birchfield
B
0

Putting down my answer as I keep coming to this question and forgetting it's been solved.

(ns my-project.handler
    (:require (compojure [handler :as handler]
                         [route :as route]
                         [core :refer :all])
              [noir.util.middleware :as middleware]
              [noir.session :as session]))

(defroutes my-routes
  (GET "/put" req (session/put! :test "yes"))
  (GET "/get" req (session/get :test))
  (route/resources "/")
  (route/not-found "Not Found"))

(def app
  (-> 
   (handler/site my-routes)
    session/wrap-noir-flash ; only if you need flash-put! and flash-get
    session/wrap-noir-session))
Bergstrom answered 11/9, 2014 at 2:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.