Files
CoHaiSe-CLOUD/frontend/src/pages/ChangePassword.tsx
T

120 lines
4.0 KiB
TypeScript

import { useState, type FormEvent } from "react";
import { useNavigate } from "react-router-dom";
import { Button, Card, CardContent, CardDescription, CardHeader, CardTitle, Input } from "../ui/index.js";
import { Alert } from "../components/feedback.js";
import { ApiError, api } from "../lib/api.js";
import { useSession } from "../lib/session.js";
import { useI18n } from "../lib/i18n.js";
export const ChangePassword = () => {
const { user, refresh, logout } = useSession();
const { t } = useI18n();
const navigate = useNavigate();
const [current, setCurrent] = useState("");
const [next, setNext] = useState("");
const [confirm, setConfirm] = useState("");
const [error, setError] = useState<string | null>(null);
const [busy, setBusy] = useState(false);
const submit = async (event: FormEvent<HTMLFormElement>) => {
event.preventDefault();
if (next !== confirm) {
setError(t("Les deux mots de passe ne correspondent pas."));
return;
}
setBusy(true);
setError(null);
try {
await api.changePassword(current, next);
await refresh();
navigate("/files", { replace: true });
} catch (caught) {
setError(caught instanceof ApiError ? caught.message : t("Changement impossible."));
} finally {
setBusy(false);
}
};
return (
<div className="w-full max-w-sm">
<header className="mb-6 text-center">
<div className="flex items-center justify-center gap-3">
<img
src="/icon.png"
alt=""
className="h-12 w-12 rounded-xl ring-1 ring-black/5"
/>
<div className="text-left">
<p className="font-display text-xs font-semibold uppercase tracking-logo text-accent">
CoHaiSe
</p>
<h1 className="font-display text-lg font-semibold uppercase tracking-label text-foreground">
Cloud
</h1>
</div>
</div>
<p className="mt-2 text-sm leading-relaxed text-muted-foreground">
{t("Bienvenue {name}. Ce mot de passe a été défini par un administrateur : choisissez le vôtre.", { name: user?.first_name ?? "" })}
</p>
</header>
<Card>
<CardHeader className="text-center">
<CardTitle>{t("Changement requis")}</CardTitle>
<CardDescription>
{t("Les autres sessions ouvertes sur ce compte seront fermées.")}
</CardDescription>
</CardHeader>
<CardContent>
<form className="flex flex-col gap-4" onSubmit={(event) => void submit(event)}>
<Input
centered
label={t("Mot de passe actuel")}
type="password"
autoComplete="current-password"
required
autoFocus
value={current}
onChange={(event) => setCurrent(event.target.value)}
/>
<Input
centered
label={t("Nouveau mot de passe")}
type="password"
autoComplete="new-password"
required
minLength={8}
hint={t("8 caractères minimum.")}
value={next}
onChange={(event) => setNext(event.target.value)}
/>
<Input
centered
label={t("Confirmation")}
type="password"
autoComplete="new-password"
required
minLength={8}
value={confirm}
onChange={(event) => setConfirm(event.target.value)}
/>
{error ? <Alert className="text-center">{error}</Alert> : null}
<Button type="submit" size="lg" fullWidth isLoading={busy}>
{t("Enregistrer")}
</Button>
<Button
type="button"
variant="ghost"
size="md"
fullWidth
onClick={() => void logout()}
>
{t("Se déconnecter")}
</Button>
</form>
</CardContent>
</Card>
</div>
);
};