We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Just a thought, one common use case you might like to cover in the state machine is refreshing an access token before doing an api request.
Here's some rough code which might explain better...
(ns app.events.api-events (:require [re-frame.core :as re-frame] [interop.anom :as anom] [connect-app.ins :as ins])) (defn login [_ _] {:app-auth/authorize {:resp-v [:api/login-response]}}) (defn login-response [{:keys [db]} [_ resp]] (cond (anom/interrupted? resp) {} (anom/anomaly? resp) {:dispatch [:app/report-anom resp]} :else {:db (assoc db :api/state resp)})) (defn with-fresh-token [{:keys [db]} [_ req]] (let [{:keys [refresh-at] :as state} (:api/state db) event-v [:api/request req]] (cond ; User not authenticated. Dispatch request without token. (nil? state) {:dispatch event-v} ; Refresh in progress. Delay dispatch. (:api/refreshing? db) {:db (update db :api/dispatch-n conj event-v)} ; Time to refresh token (> (js/Date.now) refresh-at) {:db (-> db (assoc-in [:api/state :refreshing?] true) (assoc-in [:api/state :dispatch-n] [event-v])) :app-auth/refresh {:api/state state :resp-v [:api/with-fresh-token-response]}} ; Token is fine, dispatch. :else {:dispatch event-v}))) (defn with-fresh-token-response [{:keys [db]} [_ resp]] (let [dispatch-n (get-in db [:api/state :dispatch-n])] (cond (anom/anomaly? resp) {:db (update-in db [:api/state] dissoc :api/refreshing?) :dispatch [:app/report-anom resp]} :else {:db (assoc-in db [:api/state] resp) :dispatch-n dispatch-n}))) (defn request [{:keys [db]} [_ req]] (let [access-token (get-in db [:api/state :accessToken])] {:ajax/request (assoc req :access-token access-token)})) (re-frame/reg-event-fx :api/login ins/std-ins login) (re-frame/reg-event-fx :api/login-response [ins/std-ins ins/db->storage] login-response) (re-frame/reg-event-fx :api/with-fresh-token ins/std-ins with-fresh-token) (re-frame/reg-event-fx :api/with-fresh-token-response [ins/std-ins ins/db->storage] with-fresh-token-response) (re-frame/reg-event-fx :api/request ins/std-ins request)
The text was updated successfully, but these errors were encountered:
No branches or pull requests
Just a thought, one common use case you might like to cover in the state machine is refreshing an access token before doing an api request.
Here's some rough code which might explain better...
The text was updated successfully, but these errors were encountered: