103 lines
3.4 KiB
TypeScript
103 lines
3.4 KiB
TypeScript
import { useState, type FormEvent } from "react";
|
|
import { Link, Navigate, useNavigate } from "react-router-dom";
|
|
import { Button, Card, CardContent, CardDescription, CardHeader, CardTitle, Input } from "../ui/index.js";
|
|
import { ApiError, api } from "../lib/api.js";
|
|
import { useSession } from "../lib/session.js";
|
|
import { useI18n } from "../lib/i18n.js";
|
|
|
|
export const Login = () => {
|
|
const { user, setUser } = useSession();
|
|
const { t } = useI18n();
|
|
const navigate = useNavigate();
|
|
const [email, setEmail] = useState("");
|
|
const [password, setPassword] = useState("");
|
|
const [error, setError] = useState<string | null>(null);
|
|
const [busy, setBusy] = useState(false);
|
|
|
|
if (user) return <Navigate to="/files" replace />;
|
|
|
|
const submit = async (event: FormEvent<HTMLFormElement>) => {
|
|
event.preventDefault();
|
|
setBusy(true);
|
|
setError(null);
|
|
try {
|
|
const { user: current } = await api.login(email, password);
|
|
setUser(current);
|
|
navigate(current.must_change_password ? "/change-password" : "/files", { replace: true });
|
|
} catch (caught) {
|
|
setError(caught instanceof ApiError ? caught.message : t("Connexion 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("Connectez-vous pour accéder à vos fichiers.")}
|
|
</p>
|
|
</header>
|
|
|
|
<Card>
|
|
<CardHeader className="text-center">
|
|
<CardTitle>{t("Connexion")}</CardTitle>
|
|
<CardDescription>{t("Votre session reste ouverte 14 jours.")}</CardDescription>
|
|
</CardHeader>
|
|
<CardContent>
|
|
<form className="flex flex-col gap-4" onSubmit={(event) => void submit(event)}>
|
|
<Input
|
|
centered
|
|
label={t("Adresse e-mail")}
|
|
type="email"
|
|
autoComplete="username"
|
|
required
|
|
autoFocus
|
|
value={email}
|
|
onChange={(event) => setEmail(event.target.value)}
|
|
/>
|
|
<Input
|
|
centered
|
|
label={t("Mot de passe")}
|
|
type="password"
|
|
autoComplete="current-password"
|
|
required
|
|
value={password}
|
|
onChange={(event) => setPassword(event.target.value)}
|
|
{...(error ? { error } : {})}
|
|
/>
|
|
|
|
<Button type="submit" size="lg" fullWidth isLoading={busy}>
|
|
{t("Se connecter")}
|
|
</Button>
|
|
</form>
|
|
</CardContent>
|
|
</Card>
|
|
|
|
<p className="mt-4 text-center text-xs text-muted-foreground">
|
|
<Link
|
|
to="/forgot-password"
|
|
className="underline-offset-2 hover:text-foreground hover:underline"
|
|
>
|
|
{t("Mot de passe oublié ?")}
|
|
</Link>
|
|
</p>
|
|
</div>
|
|
);
|
|
};
|