From a07ae9b2641d2d9c91b66f6a54eb4e7db5504722 Mon Sep 17 00:00:00 2001 From: 1xDed Date: Fri, 27 Nov 2026 14:40:51 +0300 Subject: [PATCH] fix: return to login when the stored token is revoked Verify the saved access token in the /_api guard (it was commented out) and clear storage/redirect to /login/manual on 401. Also catch fetchCurrentUser failures during startup. --- src/main.tsx | 13 +++++++++++-- src/routes/_api.tsx | 36 +++++++++++++++++++++--------------- 2 files changed, 32 insertions(+), 17 deletions(-) diff --git a/src/main.tsx b/src/main.tsx index 12af062..8aa3db4 100644 --- a/src/main.tsx +++ b/src/main.tsx @@ -10,6 +10,7 @@ import { useApiInContext, } from "./utils/store/api"; import { CentralProvider, useCentralStore } from "./utils/store/central"; +import { delUser } from "./utils/storage/user"; const queryClient = new QueryClient({ defaultOptions: { @@ -67,9 +68,17 @@ function ProviderWrapper() { ]); useEffect(() => { if (api?.accessToken && !user?.Id) { - fetchCurrentUser(api); + fetchCurrentUser(api).catch(async (error) => { + console.error("Could not restore the saved session", error); + await delUser(); + createApi(api.basePath, undefined); + router.navigate({ + to: "/login/manual", + search: { redirect: window.location.pathname }, + }); + }); } - }, [api, user, fetchCurrentUser]); + }, [api, user, fetchCurrentUser, createApi]); useEffect(() => { if (api) { console.log(`API is set - Route ${window.location.pathname}`); diff --git a/src/routes/_api.tsx b/src/routes/_api.tsx index ed6782b..202b719 100644 --- a/src/routes/_api.tsx +++ b/src/routes/_api.tsx @@ -1,5 +1,5 @@ import { getDefaultServer, getServer } from "@/utils/storage/servers"; -import { getUser } from "@/utils/storage/user"; +import { delUser, getUser } from "@/utils/storage/user"; import { getSystemApi } from "@jellyfin/sdk/lib/utils/api/system-api"; import { getUserApi } from "@jellyfin/sdk/lib/utils/api/user-api"; import { createFileRoute, redirect } from "@tanstack/react-router"; @@ -56,20 +56,26 @@ export const Route = createFileRoute("/_api")({ } } } else if (context.api) { - // if (context.api.accessToken && location.pathname !== "/login/manual") { - // try { - // console.info("AccessToken", context.api.accessToken); - // await getUserApi(context.api).getCurrentUser(); // Verify user is able to authenticate - // } catch (error) { - // console.error(error); - // throw redirect({ - // to: "/login/manual", - // search: { - // redirect: location.href, - // }, - // }); - // } - // } + // The API was created from a token stored on disk. The token can be + // revoked (logged out everywhere / server reset); verify it and send + // the user back to the login page instead of leaving a broken page. + const isAuthPage = + location.pathname.startsWith("/login") || + location.pathname.startsWith("/setup") || + location.pathname.startsWith("/error"); + if (context.api.accessToken && !isAuthPage) { + try { + await getUserApi(context.api).getCurrentUser(); + } catch (error) { + console.error("Stored access token is no longer valid", error); + await delUser(); + context.createApi(context.api.basePath, undefined); + throw redirect({ + to: "/login/manual", + search: { redirect: location.href }, + }); + } + } } }, }); \ No newline at end of file