diff --git a/src/main.tsx b/src/main.tsx index c3f4b43..eff775c 100644 --- a/src/main.tsx +++ b/src/main.tsx @@ -70,6 +70,10 @@ function ProviderWrapper() { if (api?.accessToken && !user?.Id) { fetchCurrentUser(api).catch(async (error) => { console.error("Could not restore the saved session", error); + const status = (error as { response?: { status?: number } })?.response + ?.status; + // Only an actual authorization failure should log the user out. + if (status !== 401 && status !== 403) return; await delUser(); createApi(api.basePath, undefined); router.navigate({ diff --git a/src/routes/_api.tsx b/src/routes/_api.tsx index 202b719..a578817 100644 --- a/src/routes/_api.tsx +++ b/src/routes/_api.tsx @@ -4,9 +4,18 @@ 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"; +const isAuthError = (error: unknown) => { + const status = (error as { response?: { status?: number } })?.response?.status; + return status === 401 || status === 403; +}; + export const Route = createFileRoute("/_api")({ beforeLoad: async ({ context, location }) => { - console.log("RUNNING API CHECK"); + const isAuthPage = + location.pathname.startsWith("/login") || + location.pathname.startsWith("/setup") || + location.pathname.startsWith("/error"); + if (!context.api) { const currentServerId = await getDefaultServer(); if (currentServerId) { @@ -36,22 +45,30 @@ export const Route = createFileRoute("/_api")({ ); try { await getUserApi(apiTemp).getCurrentUser(); - // return { user: user.data }; } catch (error) { console.error(error); - throw redirect({ - to: "/login/manual", - search: { - redirect: location.href, - }, - }); + if (isAuthError(error)) { + await delUser(); + throw redirect({ + to: "/login/manual", + search: { + redirect: location.href, + }, + }); + } + // Transient network failure: keep the saved session + // instead of logging the user out. } } context.createApi(currentServer?.address, userOnDisk.AccessToken); } else { context.createApi(currentServer?.address, undefined); // Creates Api - - // return { api: apiTemp }; + if (!isAuthPage) { + throw redirect({ + to: "/login/manual", + search: { redirect: location.href }, + }); + } } } } @@ -59,21 +76,26 @@ export const Route = createFileRoute("/_api")({ // 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) { + throw redirect({ + to: "/login/manual", + search: { redirect: location.href }, + }); + } 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 }, - }); + console.error("Could not verify the stored access token", error); + if (isAuthError(error)) { + await delUser(); + context.createApi(context.api.basePath, undefined); + throw redirect({ + to: "/login/manual", + search: { redirect: location.href }, + }); + } + // Network hiccup: do not throw the user out of the app. } } }