2

I have an endpoint called /account which provides user info(returns html).

When unauthorised user tries to access this endpoint I need to be able to redirect to login page but in Liberator I found post-redirect so far and it is just for post methods.

I need to redirect get methods as well, how can I achieve this?

4

1 回答 1

3

我发现以下代码的解决方法可以解决问题:

(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?并返回登录 html,:handle-unauthorized但我怀疑这是否是一个好习惯。

于 2017-04-07T09:31:34.953 回答