问题
This is how I save it during login:
(defn set-loggedin [username]
(sesh/put! :username username))
(defn login-handler [username password]
(let [user (datab/login username password)]
(if (empty? user)
(view/login-form "Wrong password.")
(do
(set-loggedin username)
(resp/redirect "/movies")))))
(defroutes app-routes
...
(POST "/" [username password] (login-handler username password))
(POST "/movie/save" [movieID name] (film-new movieID name))
...)
(def app
(noir-middleware/app-handler
[app-routes]
:ring-defaults (assoc-in site-defaults [:security :anti-forgery] false)))
When I show the username on the form after the login, it shows it, but when I try to save the movie (movie table has a column username) and get the username it is nil.
(defn film-new [movieID name]
(datab/filmnew movieID name (sesh/get :username))
(resp/redirect "/movies")
)
Am i wrapping the session wrong? I don't get it. I have also tried to wrap it like this
def app (sesh/wrap-noir-session*
(noir-middleware/app-handler [app-routes]
:ring-defaults (assoc-in site-defaults [:security :anti-forgery]
false))))
回答1:
EDIT: libnoir is now officially deprecated. Start here.
This is a minimal example to show that libnoir's stateful session works out of the box:
(ns ln.core
(:require
[compojure.core :refer [defroutes GET]]
[noir.session :as session]
[noir.util.middleware :as middleware]
[ring.adapter.jetty :refer [run-jetty]]))
(defroutes app-routes
(GET "/foo" []
(session/update-in! [:foo] not)
"Now go to /bar")
(GET "/bar" []
"foo was" (str (session/get :foo))))
(def app
(middleware/app-handler
[app-routes]))
(defonce server (atom nil))
(defn stop-server []
(when-let [s @server]
(.stop s)))
(defn dev []
(stop-server)
(reset! server (run-jetty app {:port 8888
:join? false})))
;;;; Scratch
(comment
(dev)
)
If you could provide a minimal reproduction to show that your code is NOT working as expected, we could help you further. Having said that, libnoir looks a bit abandoned to me. You might be better of starting with vanilla Ring.
来源:https://stackoverflow.com/questions/52334131/why-is-username-not-saved-in-noir-session-in-clojure-project