如何在compojure Web项目的noir会话中保存用户?

时间:2018-09-11 22:05:01

标签: clojure compojure ring noir lib-noir

我必须在会话中保存用户名,但似乎无法使其正常工作。这是我的代码(sesh是noir会话):

(defn set-loggedin [username password]
(do
(sesh/put! :username (:user username))
(sesh/put! :password (:pass password))))

(defn login-handler [username password]
(let [user (datab/login username password)]
(if (empty? user)
(view/login-form "Wrong username/password")
(do
(set-loggedin username password)
(response/redirect "/main")))))

(defroutes app-routes
(GET "/" [] (view/login-form "" ))
(POST "/" [username password] (login-handler username password))
(GET "/main" [] (view/main-page))
(route/resources "/")
(route/not-found "Not Found"))

(def app
(sesh/wrap-noir-session
(wrap-defaults  app-routes (assoc-in site-defaults [:security :anti-forgery] 
false))))

这是打ic的主页,并且在我登录时不显示用户:

(defn main-page []
(html[:head [:title "main"]] [:body
                             (sesh/get :username)] ))

任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:0)

在您的set-loggedin函数中,您尝试从'username'中获取:user密钥,我怀疑它是字符串,而从'password'中获取:pass密钥,可能是字符串。

在函数定义中也不需要做该操作,所以请尝试以下操作:

(defn set-loggedin [username password]
  (sesh/put! :username username)
  (sesh/put! :password password)))