Why is username not saved in noir session in Clojure project?

浪尽此生 提交于 2019-12-11 15:45:21

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!