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
+47 -21

No files matched your search

+4
View File
@@ -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({
+43 -21
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 { 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.
}
}
}