fix: auto-redirect to login on 401 Unauthorized

When API returns 401, clear stored tokens and redirect to /login
instead of showing an error message.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
hailin 2026-03-02 09:40:58 -08:00
parent 375c5632f6
commit d21f41d7c3
1 changed files with 7 additions and 0 deletions

View File

@ -42,6 +42,13 @@ export async function apiClient<T>(endpoint: string, options: RequestOptions = {
});
if (!response.ok) {
if (response.status === 401 && typeof window !== 'undefined') {
localStorage.removeItem('access_token');
localStorage.removeItem('refresh_token');
localStorage.removeItem('current_tenant');
window.location.href = '/login';
throw new Error('Session expired');
}
const errorData = await response.json().catch(() => null);
const message = errorData?.message || `${response.status} ${response.statusText}`;
throw new Error(message);