fix session invalidation on invalid credentials (#2230)

This commit is contained in:
jeffvli
2026-07-18 23:51:52 -07:00
parent a378699b12
commit 61eb998de1
5 changed files with 80 additions and 17 deletions
+15 -4
View File
@@ -4,6 +4,7 @@ import qs from 'qs';
import { z } from 'zod';
import i18n from '/@/i18n/i18n';
import { authenticationFailure } from '/@/renderer/api/utils';
import { useAuthStore } from '/@/renderer/store';
import { getServerUrl } from '/@/renderer/utils/normalize-server-url';
import { ssType } from '/@/shared/api/subsonic/subsonic-types';
@@ -12,6 +13,8 @@ import { toast } from '/@/shared/components/toast/toast';
import { ServerListItemWithCredential } from '/@/shared/types/domain-types';
import { ServerFeature } from '/@/shared/types/features-types';
const SUBSONIC_AUTH_ERROR_CODE = 40;
const c = initContract();
export const contract = c.router({
@@ -392,17 +395,25 @@ axiosClient.interceptors.response.use(
if (data['subsonic-response'].status !== 'ok') {
// Suppress code related to non-linked lastfm or spotify from Navidrome
if (data['subsonic-response'].error.code !== 0) {
const isAuthenticated = Boolean(useAuthStore.getState().currentServer?.credential);
const currentServer = useAuthStore.getState().currentServer;
const isAuthenticated = Boolean(currentServer?.credential);
const errorCode = data['subsonic-response'].error.code;
const errorMessage = data['subsonic-response'].error.message as string | undefined;
// Servers may return code as string ("40") — coerce before comparing
const numericCode = Number(errorCode);
const isAuthError = numericCode === SUBSONIC_AUTH_ERROR_CODE;
if (isAuthenticated) {
if (isAuthenticated && isAuthError) {
authenticationFailure(currentServer, errorMessage);
} else if (isAuthenticated) {
toast.error({
message: data['subsonic-response'].error.message,
message: errorMessage,
title: i18n.t('error.genericError') as string,
});
}
// Since we do status === 200, override this value with the error code
response.status = data['subsonic-response'].error.code;
response.status = numericCode || errorCode;
}
}
+35 -8
View File
@@ -1,17 +1,44 @@
import isElectron from 'is-electron';
import i18n from '/@/i18n/i18n';
import { useAuthStore } from '/@/renderer/store';
import { toast } from '/@/shared/components/toast/toast';
import { ServerListItem } from '/@/shared/types/types';
export const authenticationFailure = (currentServer: null | ServerListItem) => {
const localSettings = isElectron() ? window.api.localSettings : null;
const AUTH_FAILURE_TOAST_ID = 'auth-failure';
export const authenticationFailure = (
currentServer: null | ServerListItem,
message?: string,
) => {
const store = useAuthStore.getState();
const serverId = currentServer?.id ?? store.currentServer?.id;
toast.error({
message: 'Your session has expired.',
id: AUTH_FAILURE_TOAST_ID,
message: message ?? (i18n.t('error.sessionExpiredError') as string),
});
if (currentServer) {
const serverId = currentServer.id;
const token = currentServer.ndCredential;
console.error(`token is expired: ${token}`);
useAuthStore.getState().actions.updateServer(serverId, { ndCredential: undefined });
useAuthStore.getState().actions.setCurrentServer(null);
if (!serverId) {
return;
}
console.error(
`token is expired: ${currentServer?.ndCredential ?? store.currentServer?.ndCredential}`,
);
localSettings?.passwordRemove(serverId);
// logout() clears credentials on the server list entry and sets currentServer to null.
// If there is no current server, still clear the matching server list entry.
if (store.currentServer) {
store.actions.logout();
} else {
store.actions.updateServer(serverId, {
credential: '',
ndCredential: undefined,
savePassword: false,
});
}
};