如何在 Clojure 中使用 Liberator 重定向 get 方法?

How to redirect get methods with Liberator in Clojure?

我有一个名为 /account 的端点,它提供用户信息 (returns html)。

当未经授权的用户尝试访问此端点时,我需要能够重定向到 login page,但在 Liberator 中,到目前为止我发现了 post-redirect,它仅用于 post methods

我也需要重定向 get methods,我该如何实现?

我发现了以下代码的解决方法:

(defn account
  []
  (resource :allowed-methods [:get]

            :available-media-types ["text/html"]

            :exists? (fn [_] false)

            :existed? (fn [_] true)

            :moved-temporarily? (fn [ctx] {:location "/redirected-path-or-url"})

            :handle-ok (fn [ctx]
                         [:html ...])

            :handle-exception (fn [_]
                                "Something went wrong")))

或者您可以检查 :authorized? 和 return 从 :handle-unauthorized 登录 html 但我怀疑这是不是一个好习惯。