clojure - 使用重新框架实现登录系统
问题描述
我是重新构建的新手,不太确定如何使用它构建用户身份验证/授权系统。
根据我收集到的信息,我应该创建一个身份验证interceptor
并将我的身份验证逻辑放在:before
部分中,然后将拦截器注入到每个事件中reg-event-db
并且reg-event-fx
我想要保护。
我在正确的轨道上吗?
解决方案
不确定我的解决方案是否特别惯用,但我在我的一个项目中使用了类似以下的内容。把它当作我的作品。
为 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-fx和ajax.core
在后端,我使用类似于我在https://github.com/ska2342/ring-routes-demo上发布的演示代码。
希望有帮助。
这篇文章中代码的许可证
除了 StackOverflow 站点的默认许可之外,我还在 Eclipse Public License 1.0 版或(由您选择)任何更高版本下发布这些行。
推荐阅读
- node.js - Nodejs函数异步
- ios - 如何在受监督的 iOS 设备上使用 NEDNSProxyProvider 将 DNS 数据包发送到自定义 DNS 服务器
- python - 如何在 librosa specshow 中显示节拍数而不是时间?
- pine-script - 如何标记过去的酒吧?
- android - 提供替代位图!如何识别最初我们需要将图像放在哪个桶/可绘制对象中
- python - 有没有办法通过 python 脚本中途结束进程或程序?
- openstreetmap - 是否有可能将 epanet 水网络集成到 thingsboard?
- javascript - 如何让 puppeteer 等待来自 Cloudflare 浏览器检查的页面重定向?
- c++ - 模板参数值函数的继承
- session - 使用 JWT 识别 Wildfly 上的服务器端会话