- Changed the 401 handling in request() to only clear the auth token when the error message indicates the token is actually invalid
- Previously: ANY 401 response cleared the token (could cause cascading failures)
- Now: Only clears on specific messages like "Token has expired", "Could not validate credentials", etc.
- "Authentication required" (which might be a timing issue) no longer clears the token

Closes #257
This commit is contained in:
maziggy
2026-02-04 14:20:12 +01:00
parent 4910bce0b2
commit 85598346f7
4 changed files with 43 additions and 9 deletions
+25 -2
View File
@@ -95,11 +95,11 @@ describe('API Client Auth Header', () => {
expect(capturedHeaders!.get('Authorization')).toBeNull();
});
it('clears token on 401 Unauthorized response', async () => {
it('clears token on 401 with invalid token message', async () => {
server.use(
http.get('/api/v1/settings/spoolman', () => {
return HttpResponse.json(
{ detail: 'Not authenticated' },
{ detail: 'Could not validate credentials' },
{ status: 401 }
);
})
@@ -117,6 +117,29 @@ describe('API Client Auth Header', () => {
expect(getAuthToken()).toBeNull();
expect(localStorageMock.removeItem).toHaveBeenCalledWith('auth_token');
});
it('does not clear token on 401 with generic auth error', async () => {
server.use(
http.get('/api/v1/settings/spoolman', () => {
return HttpResponse.json(
{ detail: 'Authentication required' },
{ status: 401 }
);
})
);
setAuthToken('valid-token');
expect(getAuthToken()).toBe('valid-token');
try {
await api.getSpoolmanSettings();
} catch {
// Expected to throw
}
// Token should NOT be cleared for generic auth errors (might be timing issue)
expect(getAuthToken()).toBe('valid-token');
});
});
describe('FormData requests include auth header', () => {