From 54feb7b2f9c9c56e1776eb3401bf95b2672670d7 Mon Sep 17 00:00:00 2001 From: Manav Rathi Date: Thu, 15 May 2025 12:20:13 +0530 Subject: [PATCH] sv --- web/apps/photos/src/pages/gallery.tsx | 15 +++++++++++++++ web/packages/base/local-user.ts | 16 +++++++++++----- 2 files changed, 26 insertions(+), 5 deletions(-) diff --git a/web/apps/photos/src/pages/gallery.tsx b/web/apps/photos/src/pages/gallery.tsx index ad52bddd26..0145788543 100644 --- a/web/apps/photos/src/pages/gallery.tsx +++ b/web/apps/photos/src/pages/gallery.tsx @@ -21,6 +21,7 @@ import { Upload, type UploadTypeSelectorIntent } from "components/Upload"; import SelectedFileOptions from "components/pages/gallery/SelectedFileOptions"; import { sessionExpiredDialogAttributes } from "ente-accounts/components/utils/dialog"; import { stashRedirect } from "ente-accounts/services/redirect"; +import { checkSessionValidity } from "ente-accounts/services/session"; import type { MiniDialogAttributes } from "ente-base/components/MiniDialog"; import { NavbarBase } from "ente-base/components/Navbar"; import { CenteredRow } from "ente-base/components/containers"; @@ -31,6 +32,7 @@ import { errorDialogAttributes } from "ente-base/components/utils/dialog"; import { useIsSmallWidth } from "ente-base/components/utils/hooks"; import { useModalVisibility } from "ente-base/components/utils/modal"; import { useBaseContext } from "ente-base/context"; +import { getAuthToken } from "ente-base/local-user"; import log from "ente-base/log"; import { clearSessionStorage, @@ -546,6 +548,19 @@ const Page: React.FC = () => { const handleSyncWithRemote = useCallback( async (force = false, silent = false) => { if (!navigator.onLine) return; + if ( + !(await getAuthToken()) || + !(await checkSessionValidity() + .then(({ status }) => status != "invalid") + .catch(() => true)) + ) { + // If we don't have an auth token, or if remote says that the + // auth token is invalid, then show the session expired dialog. + // Ignore other errors since we don't want to log the user out + // on e.g. transient network issues. + showSessionExpiredDialog(); + return; + } if (!(await masterKeyFromSessionIfLoggedIn())) { clearSessionStorage(); router.push("/credentials"); diff --git a/web/packages/base/local-user.ts b/web/packages/base/local-user.ts index c0e48a4d19..4552b07959 100644 --- a/web/packages/base/local-user.ts +++ b/web/packages/base/local-user.ts @@ -47,19 +47,25 @@ export const ensureLocalUser = (): LocalUser => { }; /** - * Return the user's auth token, or throw an error. + * Return the user's auth token, if present. * * The user's auth token is stored in KV DB after they have successfully logged * in. This function returns that saved auth token. * - * If no such token is found (which should only happen if the user is not logged - * in), then it throws an error. - * * The underlying data is stored in IndexedDB, and can be accessed from web * workers. */ +export const getAuthToken = () => getKVS("token"); + +/** + * Return the user's auth token, or throw an error. + * + * The user's auth token can be retrieved using {@link getAuthToken}. This + * function is a wrapper which throws an error if the token is not found (which + * should only happen if the user is not logged in). + */ export const ensureAuthToken = async () => { - const token = await getKVS("token"); + const token = await getAuthToken(); if (!token) throw new Error("Not logged in"); return token; };