batchCooking/apps/admin-web/src/features/auth/AdminAuthContext.tsx
Nicolas 8806b868b9 feat(admin): scaffold de l'application d'administration apps/admin-web
Nouvelle app Vite/React independante (workspace apps/*), calquee sur
apps/web : port 5174, sa propre image Docker (nginx statique), son propre
domaine/deploiement.

- AdminApiClient (VITE_ADMIN_API_URL, credentials: include) + ApiError.
- AdminAuthContext / RequireAdmin : restaure la session admin via
  GET /admin/auth/me, garde de route (miroir de AuthContext/RequireAuth).
- LoginPage (/login) : validation cliente via adminLoginSchema partage,
  erreurs traduites via ErrorMessageService.
- AdminLayout : sidebar (Tableau de bord / Monitoring / Corrections) +
  deconnexion, rendu une fois autour du groupe RequireAdmin.
- Pages Dashboard / Monitoring / Corrections en placeholder (remplies aux
  PR 3-5).
- i18n fr (bloc admin.* + sous-ensemble errors.*), tokens _theme.scss
  copies de apps/web (extraction en package partage : suivi separe).
- Dockerfile multi-stage (node build -> nginx:alpine) + nginx.conf (SPA
  fallback). Service admin-web dans docker-compose.yml (port ADMIN_WEB_PORT,
  VITE_ADMIN_API_URL en build arg).
- Cypress : login.feature (KO -> message, OK -> dashboard) + admin-layout
  .cy.ts (redirection /login sans session, nav entre sections, logout).
  Job "Run admin-web E2E tests" ajoute a ci.yml.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-08-28 19:05:40 +02:00

71 lines
2.6 KiB
TypeScript

import type { AdminLoginInput, AdminUserView } from "@batch-cooking/shared";
import { createContext, type ReactNode, useCallback, useContext, useEffect, useState } from "react";
import { adminApiClient } from "../../api/client";
/** Auth state/actions exposed via {@link useAdminAuth} — the admin-app counterpart of apps/web's `AuthContext`. */
interface AdminAuthContextValue {
/** Currently authenticated admin, or `null` if no active session. */
admin: AdminUserView | null;
/** True only while the initial `GET /admin/auth/me` check is pending — lets `RequireAdmin` avoid a premature redirect. */
isLoading: boolean;
/** Verifies credentials and updates `admin` on success. Throws `ApiError` on failure. */
login: (input: AdminLoginInput) => Promise<void>;
/** Ends the session and clears `admin`. */
logout: () => Promise<void>;
}
const AdminAuthContext = createContext<AdminAuthContextValue | null>(null);
/**
* Provides admin authentication state to the whole app. On mount, calls
* `GET /admin/auth/me` once to restore the session from the `admin_session`
* httpOnly cookie (if any) — same "reload keeps you logged in" behaviour as
* apps/web's `AuthProvider`.
*/
export function AdminAuthProvider({ children }: { children: ReactNode }) {
const [admin, setAdmin] = useState<AdminUserView | null>(null);
const [isLoading, setIsLoading] = useState(true);
useEffect(() => {
adminApiClient
.me()
.then(setAdmin)
// No/invalid session — the normal state for a first visit, not an error.
.catch(() => setAdmin(null))
.finally(() => setIsLoading(false));
}, []);
const login = useCallback(async (input: AdminLoginInput) => {
try {
setAdmin(await adminApiClient.login(input));
} catch (err) {
// Rethrown as-is — `LoginPage`'s submit handler catches and displays
// it; this callback just isn't allowed a bare `await`.
throw err;
}
}, []);
const logout = useCallback(async () => {
try {
await adminApiClient.logout();
setAdmin(null);
} catch (err) {
throw err; // see login()'s catch comment
}
}, []);
return (
<AdminAuthContext.Provider value={{ admin, isLoading, login, logout }}>
{children}
</AdminAuthContext.Provider>
);
}
/** Reads the current admin auth state/actions. Must be called within an {@link AdminAuthProvider}. */
export function useAdminAuth(): AdminAuthContextValue {
const ctx = useContext(AdminAuthContext);
if (!ctx) {
throw new Error("useAdminAuth must be used within an AdminAuthProvider");
}
return ctx;
}