如何从LuminusWeb中的会话登录用户的ID

时间:2016-11-22 09:30:40

标签: clojure luminus

我刚刚在会话中保存了当前登录的用户。现在如何从 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吗?

1 个答案:

答案 0 :(得分:1)

您可以使用Luminus提供的wrap-identity中间件,确保它位于wrap-base堆叠中,然后将:id更改为:identity session。您现在可以拨打(layout/render "home.html")(即使不提供操作!),您也可以使用{{identity}}

访问该身份