re-frame

Saket 2023-10-03T19:24:37.617589Z

Sorry for newb question: I am using reframe-http-fx for http calls to my api. I want to write a response interceptor, so when the API returns a 401 error, the app redirects the user to the login page. Could you guys help me with this. I was thinking of writing an effect handler that wraps http-xhrio. Am I thinking in the right direction?

Saket 2023-10-07T13:17:39.181219Z

@mail990, your proposed solution works beautifully. Thanks for the help all of you guys. šŸ™

šŸ‘ 1
Marius 2023-10-07T16:27:14.267579Z

Happy to be able to help. Usually Iā€˜m the one asking questions šŸ˜€

p-himik 2023-10-03T20:09:15.216239Z

I would recommend creating your own effect that relies on js/fetch. It's really trivial to do, just a handful of lines. And you'll be able to wrap anything in any way you like.

2023-10-03T22:52:34.850919Z

@saket.is.sam One event will create the request (via an effect). But the HTTP response is a second event. In fact, there might be one event for success and one for failure. The event handler for the failure path should do the redirect you mention. I'm not sure what a "response interceptor" is.

Saket 2023-10-04T05:33:11.257379Z

@p-himik, Thanks for the suggestion. I am using a luminus template that has preconfigured http-fx with some interceptors that I don't want to write myself. If I don't get this working easily with http-fx, I'll certainly look into writing my own effect.

Saket 2023-10-04T05:36:29.622019Z

@mikethompson, I can do something like {:on-failure [::unauthenticated-check [::handle-failure]]} in all the http calls. Having said that, I don't want to add this to all the calls manually, I want this authenticated-check to happen on each response (hence I called it an interceptor, that takes the error response returned by the api and performs some logic before it actually is handled by application logic). Let me know if I can explain it better.

Marius 2023-10-04T06:50:43.510939Z

I extended my luminus app like this (some while ago, not sure what is my change and what was included, please ask if it does not work): ajax.cljs

(defn load-interceptors! []
  (swap! ajax/default-interceptors
         conj
         (ajax/to-interceptor {:name "default headers"
                               :request default-headers})
         (ajax/to-interceptor {:name "catch code 403"
                               :response logout-on-code-403})))
load-interceptors! is called from core.cljs in function init! (should be already there)

Marius 2023-10-04T06:51:24.818979Z

function logout-on-code-403 is as follows:

(defn logout-on-code-403 [response]
  (when (= 403 (protocols/-status response))
    (rf/dispatch [:auth/frontend-logout]))
  response)

Saket 2023-10-04T07:44:44.032799Z

Wow. I did try something like this which didn't work. As it's working for your project, I may have missed some step. Let me try again. Thanks a lot for this @mail990

2023-10-04T07:45:25.353249Z

Here's an example program whch might give you clues https://github.com/jacekschae/conduit