SurfSense/surfsense_web/lib/auth-utils.ts
Claude f536b13d13
Replace Chinese with Latvian translations and address PR review feedback
Translations:
- Add complete Latvian translations (lv.json)
- Remove Chinese translations (zh.json)
- Update i18n routing to support en/lv locales
- Update LanguageSwitcher component for Latvian
- Update LocaleContext to use Latvian messages

PR Review Improvements:
- Create shared handleSessionExpired() utility in auth-utils.ts
- Refactor use-user.ts, use-chats.ts, use-search-space.ts to use shared utility
- Add session_expired error message to auth-errors.ts
- Consistent redirect to /login?error=session_expired
2025-11-18 21:42:34 +00:00

43 lines
1.4 KiB
TypeScript

/**
* Authentication utility functions for session management
*/
import { baseApiService } from "./apis/base-api.service";
/**
* Handle session expiration by clearing tokens and redirecting to login
* This centralizes the 401 handling logic to avoid code duplication
*/
export function handleSessionExpired(): never {
// Clear token from localStorage
localStorage.removeItem("surfsense_bearer_token");
// Clear token from baseApiService singleton to prevent stale auth state
baseApiService.setBearerToken("");
// Redirect to login with error parameter for user feedback
window.location.href = "/login?error=session_expired";
// Throw to stop further execution (this line won't actually run due to redirect)
throw new Error("Session expired: Redirecting to login page");
}
/**
* Check if a response indicates an authentication error
* @param response - The fetch Response object
* @returns True if the response status is 401
*/
export function isUnauthorizedResponse(response: Response): boolean {
return response.status === 401;
}
/**
* Handle API response with automatic session expiration handling
* @param response - The fetch Response object
* @throws Error if response is 401 (after handling session expiration)
*/
export function handleAuthResponse(response: Response): void {
if (isUnauthorizedResponse(response)) {
handleSessionExpired();
}
}