通过重构实现登录系统

Ada*_*dam 5 clojure clojurescript re-frame

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

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

我走在正确的轨道上吗?

Ste*_*sen 3

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

为 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]})
Run Code Online (Sandbox Code Playgroud)

然后我使用 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))]})))
Run Code Online (Sandbox Code Playgroud)

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

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

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

 (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))))
Run Code Online (Sandbox Code Playgroud)

:received-documentation是由一些调用正确显示函数的代码来处理的。

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

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

希望有帮助。

这篇文章中代码的许可

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