fix(auth): do not log out on transient network errors
Continuous Integration / build_web (push) Failing after 0s
Continuous Integration / build (macos-latest) (push) Skipped
Continuous Integration / build (ubuntu-22.04) (push) Skipped
Continuous Integration / build (windows-11-arm) (push) Skipped
Continuous Integration / build (windows-latest) (push) Skipped
Continuous Integration / nightly_release (push) Skipped

Only 401/403 clears the saved session; other failures keep the token. Routes without a token now redirect to login instead of showing a permanent spinner.
This commit is contained in:
1xDed committed 2026-11-28 01:44:00 +03:00
1 parent ae5a2133fa
commit 75b4ddd598
2 files changed
+48 -22

No files matched your search

+4
View File
@@ -70,6 +70,10 @@ function ProviderWrapper() {
if (api?.accessToken && !user?.Id) { if (api?.accessToken && !user?.Id) {
fetchCurrentUser(api).catch(async (error) => { fetchCurrentUser(api).catch(async (error) => {
console.error("Could not restore the saved session", 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(); await delUser();
createApi(api.basePath, undefined); createApi(api.basePath, undefined);
router.navigate({ router.navigate({
+44 -22
View File
@@ -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 { getUserApi } from "@jellyfin/sdk/lib/utils/api/user-api";
import { createFileRoute, redirect } from "@tanstack/react-router"; 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")({ export const Route = createFileRoute("/_api")({
beforeLoad: async ({ context, location }) => { 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) { if (!context.api) {
const currentServerId = await getDefaultServer(); const currentServerId = await getDefaultServer();
if (currentServerId) { if (currentServerId) {
@@ -36,9 +45,10 @@ export const Route = createFileRoute("/_api")({
); );
try { try {
await getUserApi(apiTemp).getCurrentUser(); await getUserApi(apiTemp).getCurrentUser();
// return { user: user.data };
} catch (error) { } catch (error) {
console.error(error); console.error(error);
if (isAuthError(error)) {
await delUser();
throw redirect({ throw redirect({
to: "/login/manual", to: "/login/manual",
search: { search: {
@@ -46,30 +56,14 @@ export const Route = createFileRoute("/_api")({
}, },
}); });
} }
// Transient network failure: keep the saved session
// instead of logging the user out.
}
} }
context.createApi(currentServer?.address, userOnDisk.AccessToken); context.createApi(currentServer?.address, userOnDisk.AccessToken);
} else { } else {
context.createApi(currentServer?.address, undefined); // Creates Api context.createApi(currentServer?.address, undefined); // Creates Api
if (!isAuthPage) {
// return { api: apiTemp };
}
}
}
} else if (context.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) {
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({ throw redirect({
to: "/login/manual", to: "/login/manual",
search: { redirect: location.href }, search: { redirect: location.href },
@@ -77,5 +71,33 @@ export const Route = createFileRoute("/_api")({
} }
} }
} }
}
} else if (context.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.
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("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.
}
}
}
}, },
}); });