reitit

yannvahalewyn 2024-08-07T10:01:11.872549Z

Hi guys, I need to do some internal re-routing in middleware. Without Reitit something like (handler (assoc req :uri "/login")) The use case is that we usually redirect to "/login" but temporarily need the root path "/" to return a 200 because because of some issues with health checks and redirects, unfortunately. The /login is non trivial, as it can be mounted by different auth providers, has QR codes with own route level middlewares, etc.. The best solution would be to be able to call into the login handler. So far I have tried replacing :uri and replacing the :reitit.core/match in the request. Alternatively I could reorganize code to expose a 'render-login' from the auth client, use JS to redirect to "/login" or any other hack will solve my issue. But I'm curious, Is there any way to do internal redirects from middleware using Reitit Ring?

valtteri 2024-08-07T11:38:27.257529Z

Maybe a stupid question, but why not return a redirect and instruct the client to call the "/login" directly?

yannvahalewyn 2024-08-07T12:06:23.886249Z

No stupid questions 🙂. It's a good point. I mentionned this here: > The use case is that we usually redirect to "/login" but temporarily need the root path "/" to return a 200 because because of some issues with health checks and redirects, unfortunately. We do redirect but for this one root path we can't atm.

yannvahalewyn 2024-08-07T12:48:31.948379Z

This has been solved. Internal redirect works as expected with (handler (assoc req :uri "/login")) middleware. I however did not realise the auth middleware I was working in was mounted on the [:data :middleware] part of the router instead of top level. This works:

(ring/ring-handler
 (ring/router 
  [...] 
  {:data {:middleware []}}) ;; Before middleware was here
 (ring/routes (ring/create-file-handler {:path "/"}))
 {:middleware
  [(fn temporary-fix-internal-redirect-for-healthcheck [handler]
     (fn [req]
       (if (and (= (:uri req) "/")
                (not (req/current-user req)))
         (handler (assoc req :uri "/login"))
         (handler req))))]})

👍 1