clojurecompojureringnoirlib-noir

How to save a user in noir session in a compojure web project?


I have to save a username in a session and I can't seem to make it work. This is my code (sesh is noir session):

(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))))

This is the main page in hiccup and it doesn't show the user when I login:

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

Any help would be appreciated!


Solution

  • In your set-loggedin function, you are trying to get the :user key from 'username', which I suspect is a string and the :pass key from 'password', which is probably a string.

    The do is also unnecessary in a function definition so try this:

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