Heya, I want to make an api call with a Bearer access token from Auth0. Is it appropriate/idiomatic in re-frame to use an effect handler for retrieving an Auth0 access token when using getTokenSilently from the auth0-spa-js library (which returns a promise)? So something like this?
(rf/reg-fx
:fetch-access-token
(fn [on-success]
(-> (.getTokenSilently client)
(.then (fn [result]
(on-success result)))
(.catch (fn [error]
(js/console.error "Error fetching access token:" error))))))
(rf/reg-event-fx
::api-call
(fn [{:keys [db]} _]
{:fetch-access-token (fn [token]
(rf/dispatch [:api-call-token token]))
:db (assoc db :api-call-pending true)}))
(rf/reg-event-fx
:api-call-token
(fn [{:keys [db]} [_ token]]
{:http-xhrio {:method :get
:uri ""
:headers {"Authorization" (str "Bearer " token)}
:timeout 8000
:response-format (ajax/json-response-format {:keywords? true})
:on-success [:success]
:on-failure [:failure]}
:db (assoc db :api-call-pending false)})) I would say yes. If something has side effects, then it belongs in a fx handler.
Thanks, I was originally feeling that adding it to coeffects with reg-cofx would be best, but I suppose the asyn nature of .getTokenSilently makes that not possible.