Consolidate frontend storage keys into constants module
- Move magic strings from AuthProvider, MainLayout, and ThemeProvider to frontend/src/utils/constants.ts - Add STORAGE_KEY_AUTHENTICATED, STORAGE_KEY_SIDEBAR_COLLAPSED, and STORAGE_KEY_THEME constants with JSDoc descriptions - Update all three files to import and use centralized keys - Prevents key drift and typo regressions across the frontend Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
@@ -44,6 +44,7 @@ import { useNavigate } from "react-router-dom";
|
||||
import * as authApi from "../api/auth";
|
||||
import { setUnauthorizedHandler } from "../api/client";
|
||||
import { setAuthErrorHandler } from "../utils/fetchError";
|
||||
import { STORAGE_KEY_AUTHENTICATED } from "../utils/constants";
|
||||
import { SessionValidationLoading } from "../components/SessionValidationLoading";
|
||||
import { useSessionValidation } from "../hooks/useSessionValidation";
|
||||
|
||||
@@ -65,8 +66,6 @@ export interface AuthContextValue {
|
||||
|
||||
export const AuthContext = createContext<AuthContextValue | null>(null);
|
||||
|
||||
const IS_AUTHENTICATED_KEY = "bangui_authenticated";
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Provider
|
||||
// ---------------------------------------------------------------------------
|
||||
@@ -87,14 +86,14 @@ export function AuthProvider({
|
||||
children: React.ReactNode;
|
||||
}): React.JSX.Element {
|
||||
const [isAuthenticated, setIsAuthenticated] = useState<boolean>(() => {
|
||||
const stored = sessionStorage.getItem(IS_AUTHENTICATED_KEY);
|
||||
const stored = sessionStorage.getItem(STORAGE_KEY_AUTHENTICATED);
|
||||
return stored === "true";
|
||||
});
|
||||
const [isValidating, setIsValidating] = useState<boolean>(isAuthenticated);
|
||||
const navigate = useNavigate();
|
||||
|
||||
const handleSessionExpired = useCallback((): void => {
|
||||
sessionStorage.removeItem(IS_AUTHENTICATED_KEY);
|
||||
sessionStorage.removeItem(STORAGE_KEY_AUTHENTICATED);
|
||||
setIsAuthenticated(false);
|
||||
navigate("/login", { replace: true });
|
||||
}, [navigate]);
|
||||
@@ -136,7 +135,7 @@ export function AuthProvider({
|
||||
|
||||
const login = useCallback(async (password: string): Promise<void> => {
|
||||
await authApi.login(password);
|
||||
sessionStorage.setItem(IS_AUTHENTICATED_KEY, "true");
|
||||
sessionStorage.setItem(STORAGE_KEY_AUTHENTICATED, "true");
|
||||
setIsAuthenticated(true);
|
||||
}, []);
|
||||
|
||||
@@ -145,7 +144,7 @@ export function AuthProvider({
|
||||
await authApi.logout();
|
||||
} finally {
|
||||
// Always clear local state even if the API call fails (e.g. expired session).
|
||||
sessionStorage.removeItem(IS_AUTHENTICATED_KEY);
|
||||
sessionStorage.removeItem(STORAGE_KEY_AUTHENTICATED);
|
||||
setIsAuthenticated(false);
|
||||
}
|
||||
}, []);
|
||||
|
||||
Reference in New Issue
Block a user