clojureliberator

How to redirect get methods with Liberator in Clojure?


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?


Solution

  • I found a workaround following code does the trick:

    (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")))
    

    Or you can check :authorized? and return login html from :handle-unauthorized but I doubt it about it's a good practice or not.