re-frame-http-fx-alpha icon indicating copy to clipboard operation
re-frame-http-fx-alpha copied to clipboard

Support token refreshing in state machine

Open olivergeorge opened this issue 5 years ago • 0 comments

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)

olivergeorge avatar Aug 08 '19 02:08 olivergeorge