This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2023-10-03
Channels
- # babashka (5)
- # beginners (34)
- # biff (3)
- # calva (29)
- # cherry (11)
- # cider (7)
- # clojure (148)
- # clojure-brasil (1)
- # clojure-europe (16)
- # clojure-nl (1)
- # clojure-norway (6)
- # clojure-uk (6)
- # clojuredesign-podcast (8)
- # clojurescript (49)
- # cursive (1)
- # datalevin (7)
- # fulcro (1)
- # honeysql (1)
- # jobs (1)
- # matrix (7)
- # off-topic (13)
- # re-frame (12)
- # react (21)
- # reagent (42)
- # releases (6)
- # remote-jobs (2)
- # shadow-cljs (9)
- # solo-full-stack (5)
- # sql (7)
- # squint (9)
- # vim (2)
- # xtdb (11)
- # yamlscript (5)
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?
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.
@U05AJ7KNDGE 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.
@U2FRKM4TW, 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.
@U051MTYAB, 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.
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)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)
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 @U02G3DBJ2SY
Here's an example program whch might give you clues https://github.com/jacekschae/conduit
Eg: https://github.com/jacekschae/conduit/blob/master/src/conduit/events.cljs#L141-L148
@U02G3DBJ2SY, your proposed solution works beautifully. Thanks for the help all of you guys. š