我刚刚在会话中保存了当前登录的用户。现在如何从 parserContext.registerBeanComponent(new BeanComponentDefinition(validatorDef, validatorName));
模板中的会话中获取用户的ID?感谢
Selmer
我已更新登录功能。它有效。
(defn login-form [{:keys [flash]}]
(layout/render
"login_form.html"
(merge (select-keys flash [:id :pass :errors]))))
(defn authenticate [id pass]
(when-let [user (db/get-user {:id id})]
(when (hashers/check pass (:pass user))
id)))
(defn login! [{:keys [params]}]
(if-let [id (authenticate (:id params) (:pass params))] ;TODO: Refactor
(do
>>>>>>>>> (update (response/found "/") :session {:id id}))
(layout/render "login_form.html" {:invalid-credentials? true})))
(defroutes auth-routes
(GET "/accounts/login/" request (login-form request))
(POST "/accounts/login/" request (login! request)))
要打印用户的ID,我已经更改了主路线。
(defn login! [{:keys [params session]}]
(if-let [id (authenticate (:id params) (:pass params))] ;TODO: Refactor
(-> (response/found "/")
(assoc :session
(assoc session :id id)))
(layout/render "login_form.html" {:invalid-credentials? true})))
现在,我可以在 (defn home-page [opts]
(layout/render "home.html" opts))
(defroutes home-routes
(GET "/" {:keys [flash session]} (home-page (or flash session))))
模板中打印用户的id
。但如果我使用任何其他网址,则用户的home.html
停止显示``。
问题>那么我需要在每条路线中传递id
吗?
答案 0 :(得分:1)
您可以使用Luminus提供的wrap-identity
中间件,确保它位于wrap-base
堆叠中,然后将:id
更改为:identity
session
。您现在可以拨打(layout/render "home.html")
(即使不提供操作!),您也可以使用{{identity}}