首页 > 解决方案 > 使用重新框架实现登录系统

问题描述

我是重新构建的新手,不太确定如何使用它构建用户身份验证/授权系统。

根据我收集到的信息,我应该创建一个身份验证interceptor并将我的身份验证逻辑放在:before部分中,然后将拦截器注入到每个事件中reg-event-db并且reg-event-fx我想要保护。

我在正确的轨道上吗?

标签: clojureclojurescriptre-frame

解决方案


不确定我的解决方案是否特别惯用,但我在我的一个项目中使用了类似以下的内容。把它当作我的作品。

为 ajax 请求创建一个映射,为错误情况设置一个特殊值(忽略该context-uri函数):

(defn xhrio-map [method path format success-event]
  {:method          method
   :uri             (context-uri path)
   :timeout         5000
   :response-format format
   :on-success      [success-event]
   :on-failure      [::ajax-failure]})

然后我对失败使用 fx 处理程序(这有点复杂,因为它还处理加载指示器):

(rf/reg-event-fx
 ::ajax-failure
 (fn [{:keys [db]} [_ http-result]]
   (if (= 403 (:status http-result))
     {:db (assoc db :loading-indicator nil)
      :dispatch [::logout]}
     {:db (assoc db :loading-indicator nil)
      :dispatch
      [::error-msg (str "Error fetching from " (:uri http-result)
                        ": " (:response http-result))]})))

事件::logout设置文档位置。这也会触发后端的注销。

(rf/reg-event-fx
 ::logout
 (fn [coefx [ev]]
   {::location "./logout"}))

最后,资源的加载是这样的:

 (defn load-with-indicator [db xhrio-data]
  {:db (assoc db :loading-indicator true)
   :http-xhrio xhrio-data})

(rf/reg-event-fx
 ::load-documentation
 (fn [{:keys [db]} _]
   (load-with-indicator
    db
    (xhrio-map :get "documentation/"
               (ajax/json-response-format {:keywords? true})
               ::received-documentation))))

由调用正确显示功能的:received-documentation一些代码处理。

这使用 day8.re-frame/http-fxajax.core

在后端,我使用类似于我在https://github.com/ska2342/ring-routes-demo上发布的演示代码。

希望有帮助。

这篇文章中代码的许可证

除了 StackOverflow 站点的默认许可之外,我还在 Eclipse Public License 1.0 版或(由您选择)任何更高版本下发布这些行。


推荐阅读