diff --git a/src/main/frontend/handler/user.cljs b/src/main/frontend/handler/user.cljs index 3e5cc63b9c..b2d4cfa7f1 100644 --- a/src/main/frontend/handler/user.cljs +++ b/src/main/frontend/handler/user.cljs @@ -8,6 +8,7 @@ [clojure.set :as set] [clojure.string :as string] [frontend.common.missionary :as c.m] + [frontend.common.thread-api :refer [def-thread-api]] [frontend.config :as config] [frontend.debug :as debug] [frontend.flows :as flows] @@ -17,7 +18,8 @@ [goog.crypt.Hmac] [goog.crypt.Sha256] [goog.crypt.base64 :as base64] - [missionary.core :as m])) + [missionary.core :as m] + [promesa.core :as p])) ;;; userinfo, token, login/logout, ... @@ -301,6 +303,11 @@ (-> (state/get-auth-id-token) parse-jwt expired?)) (throw (ex-info "empty or expired token and refresh failed" {:type :expired-token}))))))) +(def-thread-api :thread-api/ensure-id&access-token + [] + (p/let [_ (js/Promise. task--ensure-id&access-token)] + {:id-token (state/get-auth-id-token)})) + ;;; user groups (defn rtc-group? diff --git a/src/main/frontend/worker/sync.cljs b/src/main/frontend/worker/sync.cljs index 6ffe40cbe5..4ecb5dc705 100644 --- a/src/main/frontend/worker/sync.cljs +++ b/src/main/frontend/worker/sync.cljs @@ -134,6 +134,28 @@ (defn- auth-token [] (worker-state/get-id-token)) +(defn- id-token-expired? + [token] + (if-not (string? token) + true + (try + (let [exp-ms (some-> token worker-util/parse-jwt :exp (* 1000))] + (or (not (number? exp-ms)) + (<= exp-ms (common-util/time-ms)))) + (catch :default _ + true)))) + +(defn- (worker-state/get-id-token) worker-util/parse-jwt @@ -1376,8 +1398,12 @@ (when-let [current @worker-state/*db-sync-client] (when (and (= (:repo current) repo) (= (:graph-id current) (:graph-id client))) - (let [updated (connect! repo current url)] - (reset! worker-state/*db-sync-client updated))))) + (-> (p/let [token ( (#'db-sync/ (#'db-sync/ (#'db-sync/