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>
85 lines
2.9 KiB
TypeScript
85 lines
2.9 KiB
TypeScript
import { adminLoginSchema, ErrorCode } from "@batch-cooking/shared";
|
|
import { type FormEvent, useState } from "react";
|
|
import { useTranslation } from "react-i18next";
|
|
import { useNavigate } from "react-router-dom";
|
|
import { ApiError } from "../../api/client";
|
|
import { useAdminAuth } from "../../features/auth/AdminAuthContext";
|
|
import "../../features/auth/admin-auth.scss";
|
|
import { fieldErrorsFrom } from "../../lib/zod-errors";
|
|
import { errorMessageService } from "../../services/error-message.service";
|
|
|
|
/**
|
|
* The admin login screen — the only unauthenticated route. Client-side
|
|
* validation via the shared `adminLoginSchema` (same rules the API
|
|
* enforces), then `POST /admin/auth/login`; any API failure is translated
|
|
* to a localized label via {@link ErrorMessageService}. Same structure as
|
|
* apps/web's `LoginPage`.
|
|
*/
|
|
export function LoginPage() {
|
|
const { login } = useAdminAuth();
|
|
const navigate = useNavigate();
|
|
const { t } = useTranslation();
|
|
|
|
const [email, setEmail] = useState("");
|
|
const [password, setPassword] = useState("");
|
|
const [fieldErrors, setFieldErrors] = useState<Record<string, string>>({});
|
|
const [formError, setFormError] = useState<string | null>(null);
|
|
const [isSubmitting, setIsSubmitting] = useState(false);
|
|
|
|
async function handleSubmit(e: FormEvent) {
|
|
e.preventDefault();
|
|
setFormError(null);
|
|
|
|
const result = adminLoginSchema.safeParse({ email, password });
|
|
if (!result.success) {
|
|
setFieldErrors(fieldErrorsFrom(result.error));
|
|
return;
|
|
}
|
|
setFieldErrors({});
|
|
|
|
setIsSubmitting(true);
|
|
try {
|
|
await login(result.data);
|
|
void navigate("/");
|
|
} catch (err) {
|
|
const code = err instanceof ApiError ? err.code : ErrorCode.INTERNAL_ERROR;
|
|
setFormError(errorMessageService.getLabel(code));
|
|
} finally {
|
|
setIsSubmitting(false);
|
|
}
|
|
}
|
|
|
|
return (
|
|
<main className="admin-auth-page">
|
|
<form className="admin-auth-card" onSubmit={handleSubmit} noValidate>
|
|
<h1>{t("admin.login.title")}</h1>
|
|
|
|
<label htmlFor="email">{t("admin.login.emailLabel")}</label>
|
|
<input
|
|
id="email"
|
|
type="email"
|
|
value={email}
|
|
onChange={(e) => setEmail(e.target.value)}
|
|
autoComplete="email"
|
|
/>
|
|
{fieldErrors.email && <p className="field-error">{fieldErrors.email}</p>}
|
|
|
|
<label htmlFor="password">{t("admin.login.passwordLabel")}</label>
|
|
<input
|
|
id="password"
|
|
type="password"
|
|
value={password}
|
|
onChange={(e) => setPassword(e.target.value)}
|
|
autoComplete="current-password"
|
|
/>
|
|
{fieldErrors.password && <p className="field-error">{fieldErrors.password}</p>}
|
|
|
|
{formError && <p className="form-error">{formError}</p>}
|
|
|
|
<button type="submit" disabled={isSubmitting}>
|
|
{isSubmitting ? t("admin.login.submitting") : t("admin.login.submit")}
|
|
</button>
|
|
</form>
|
|
</main>
|
|
);
|
|
}
|