diff --git a/src/main/frontend/components/file_sync.cljs b/src/main/frontend/components/file_sync.cljs index ea4e11b61..010abaa15 100644 --- a/src/main/frontend/components/file_sync.cljs +++ b/src/main/frontend/components/file_sync.cljs @@ -370,9 +370,13 @@ (state/pub-event! [:file-sync/onboarding-tip :unavailable]) ;; current graph belong to other user, do nothing - (and (first @graphs-txid) - (not (fs-sync/check-graph-belong-to-current-user (user-handler/user-uuid) - (first @graphs-txid)))) + (let [user-uuid (async/path-map (zipmap encrypted-path-list* path-list-or-exp)] - (set - (mapv - #(->FileMetadata (:size %) - (:checksum %) - (get encrypted-path->path-map (:encrypted-path %)) - (:encrypted-path %) - (:last-modified %) - true nil) - file-meta-list*))))))))) + (user/path-map (zipmap encrypted-path-list* path-list-or-exp)] + (set + (mapv + #(->FileMetadata (:size %) + (:checksum %) + (get encrypted-path->path-map (:encrypted-path %)) + (:encrypted-path %) + (:last-modified %) + true nil) + file-meta-list*))))))))) (path-map (zipmap encrypted-paths paths-or-exp)] - (into #{} - (comp - (filter #(not= "filepath too long" (:Error %))) - (map #(->FileMetadata (:Size %) - (:Checksum %) - (get encrypted-path->path-map (:FilePath %)) - (:FilePath %) - (:LastModified %) - true nil))) - r)))))))) + (user/path-map (zipmap encrypted-paths paths-or-exp)] + (into #{} + (comp + (filter #(not= "filepath too long" (:Error %))) + (map #(->FileMetadata (:Size %) + (:Checksum %) + (get encrypted-path->path-map (:FilePath %)) + (:FilePath %) + (:LastModified %) + true nil))) + r)))))))) ( {} - (seq graph-name-opt) - (assoc :GraphName graph-name-opt) - (seq graph-uuid-opt) - (assoc :GraphUUID graph-uuid-opt)))) + (user/ {} + (seq graph-name-opt) + (assoc :GraphName graph-name-opt) + (seq graph-uuid-opt) + (assoc :GraphUUID graph-uuid-opt)))))) (path-map - (zipmap - encrypted-paths - (path-map %))) - txns-with-encrypted-paths)] - txns))))) + (user/path-map + (zipmap + encrypted-paths + (path-map %))) + txns-with-encrypted-paths)] + txns))))) ( from-path remove-user-graph-uuid-prefix) - checksum]) - (:TXContent txn)))) - txns-with-encrypted-paths) + (user/ from-path remove-user-graph-uuid-prefix) + checksum]) + (:TXContent txn)))) + txns-with-encrypted-paths) + encrypted-paths + (mapcat + (fn [txn] + (remove + #(or (nil? %) (not (string/starts-with? % "e."))) + (mapcat + (fn [[to-path from-path _checksum]] [to-path from-path]) + (:TXContent txn)))) + txns-with-encrypted-paths*) + encrypted-path->path-map + (zipmap encrypted-paths - (mapcat - (fn [txn] - (remove - #(or (nil? %) (not (string/starts-with? % "e."))) - (mapcat - (fn [[to-path from-path _checksum]] [to-path from-path]) - (:TXContent txn)))) - txns-with-encrypted-paths*) - encrypted-path->path-map - (zipmap - encrypted-paths - (path-map to-path to-path) - (some->> from-path (get encrypted-path->path-map)) - checksum]) - (:TXContent txn)))) - txns-with-encrypted-paths*)] - [txns - (:TXId (last txns)) - (:TXId (first txns))]))))) + (path-map to-path to-path) + (some->> from-path (get encrypted-path->path-map)) + checksum]) + (:TXContent txn)))) + txns-with-encrypted-paths*)] + [txns + (:TXId (last txns)) + (:TXId (first txns))]))))) (RemoteAPI nil)) @@ -3049,43 +3040,44 @@ (when (false? @*sync-entered?) (reset! *sync-entered? true) (let [*sync-state (atom (sync-state)) - current-user-uuid (user/user-uuid) + current-user-uuid (c (persist-var/-load graphs-txid))) [user-uuid graph-uuid txid] @graphs-txid txid (or txid 0) repo (state/get-current-repo)] - (when (and repo - (graph-sync-off? repo) @network-online-cursor - user-uuid graph-uuid txid - (user/logged-in?) - (not (config/demo-graph? repo))) - (try - (when-let [sm (sync-manager-singleton current-user-uuid graph-uuid - (config/get-repo-dir repo) repo - txid *sync-state)] - (when (check-graph-belong-to-current-user current-user-uuid user-uuid) - (if-not (local-full-sync-chan true) - (offer! full-sync-chan true))))) - (catch :default e - (prn "Sync start error: ") - (log/error :exception e)))) + (offer! remote->local-full-sync-chan true) + (offer! full-sync-chan true))))) + (catch :default e + (prn "Sync start error: ") + (log/error :exception e))))) (reset! *sync-entered? false))))) ;;; ### some add-watches diff --git a/src/main/frontend/handler.cljs b/src/main/frontend/handler.cljs index 2f61f27db..8715899d1 100644 --- a/src/main/frontend/handler.cljs +++ b/src/main/frontend/handler.cljs @@ -236,7 +236,6 @@ (el/listen!)) (persist-var/load-vars) (user-handler/restore-tokens-from-localstorage) - (user-handler/refresh-tokens-loop) (js/setTimeout instrument! (* 60 1000))) (defn stop! [] diff --git a/src/main/frontend/handler/file_sync.cljs b/src/main/frontend/handler/file_sync.cljs index 90493e56e..2ca3ef323 100644 --- a/src/main/frontend/handler/file_sync.cljs +++ b/src/main/frontend/handler/file_sync.cljs @@ -44,28 +44,33 @@ [name] (go (let [r* ( (state/get-auth-id-token) parse-jwt :sub)) (defn logged-in? [] - (boolean - (some-> - (state/get-auth-id-token) - parse-jwt - expired? - not))) + (some? (state/get-auth-refresh-token))) (defn- set-token-to-localstorage! ([id-token access-token] @@ -83,12 +79,19 @@ (js/localStorage.setItem "refresh-token" refresh-token))) (defn- clear-tokens - [] - (state/set-auth-id-token nil) - (state/set-auth-access-token nil) - (state/set-auth-refresh-token nil) - (set-token-to-localstorage! "" "" "")) - + ([] + (state/set-auth-id-token nil) + (state/set-auth-access-token nil) + (state/set-auth-refresh-token nil) + (set-token-to-localstorage! "" "" "")) + ([except-refresh-token?] + (state/set-auth-id-token nil) + (state/set-auth-access-token nil) + (when-not except-refresh-token? + (state/set-auth-refresh-token nil)) + (if except-refresh-token? + (set-token-to-localstorage! "" "") + (set-token-to-localstorage! "" "" "")))) (defn- set-tokens! ([id-token access-token] @@ -109,8 +112,25 @@ (when-let [refresh-token (state/get-auth-refresh-token)] (let [resp ( 500 (:status resp))) + ;; invalid refresh-token + (clear-tokens) + + ;; e.g. api return 500, server internal error + ;; we shouldn't clear tokens if they aren't expired yet + ;; the `refresh-tokens-loop` will retry soon + (and (not (http/unexceptional-status? (:status resp))) + (not (-> (state/get-auth-id-token) parse-jwt expired?))) + nil ; do nothing + + (not (http/unexceptional-status? (:status resp))) + (clear-tokens true) + + :else ; ok (when (and (:id_token (:body resp)) (:access_token (:body resp))) - (set-tokens! (:id_token (:body resp)) (:access_token (:body resp)))))))) + (set-tokens! (:id_token (:body resp)) (:access_token (:body resp))))))))) (defn restore-tokens-from-localstorage "Restore id-token, access-token, refresh-token from localstorage, @@ -148,22 +168,25 @@ (clear-tokens) (state/pub-event! [:user/logout])) +(defn (state/get-auth-id-token) parse-jwt almost-expired-or-expired?)) + (debug/pprint (str "refresh tokens... " (tc/to-string (t/now)))) + ( (state/get-auth-id-token) parse-jwt expired?)) + (ex-info "empty or expired token and refresh failed" {}))))) +(defn id-token (parse-jwt) (almost-expired-or-expired?))) - (debug/pprint (str "refresh tokens... " (tc/to-string(t/now)))) - (