This commit is contained in:
Manav Rathi
2025-05-15 12:20:13 +05:30
parent f9dad575ec
commit 54feb7b2f9
2 changed files with 26 additions and 5 deletions

View File

@@ -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");

View File

@@ -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;
};