重新架构登录系统

时间:2018-07-15 15:22:39

标签: clojure clojurescript re-frame

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

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

我在正确的轨道上吗?

1 个答案:

答案 0 :(得分:1)

不确定我的解决方案是否特别惯用,但是我在其中一个项目中使用了以下内容。认为这是对我有用的作品。

为ajax请求创建一个映射,该映射具有针对错误情况的特殊值(忽略public class GlobalExceptionFilter : IExceptionFilter { public void OnException(ExceptionContext context) { Exception exception = context.Exception; string errorMessage; int statusCode = 500; if (exception is BusinessException) { statusCode = 400; errorMessage = exception.Message; } else { errorMessage = "Internal Error"; } var response = new ErrorResponse { Message = errorMessage }; context.Result = new ObjectResult(response) { StatusCode = statusCode, DeclaredType = typeof(ErrorResponse), ContentTypes = new MediaTypeCollection { "application/json" } }; } } 函数):

context-uri

然后我使用fx处理程序来处理失败(这更加复杂,因为它也处理加载指示符):

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

(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)))) 由某些调用正确显示功能的代码处理。

这使用day8.re-frame/http-fx:received-documentation

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

希望有帮助。

这篇文章中的代码许可

除了StackOverflow站点的默认许可证之外,我还根据Eclipse公共许可证1.0版或以后的任何版本发布这些行。