mirror of
http://88.130.71.182:3000/BlitTech/badoHair_fe.git
synced 2026-06-13 11:03:02 +00:00
Update May 12 by Elvis
This commit is contained in:
@@ -1,22 +1,46 @@
|
||||
"use client";
|
||||
|
||||
import { useState } from "react";
|
||||
import { useState, useEffect } from "react";
|
||||
import { useRouter } from "next/navigation";
|
||||
import { Button } from "@/components/ui/button";
|
||||
import { Input } from "@/components/ui/input";
|
||||
import { Label } from "@/components/ui/label";
|
||||
import { useLanguage } from "@/contexts/LanguageContext";
|
||||
import { useAuth } from "@/contexts/AuthContext";
|
||||
import { ApiError } from "@/lib/api";
|
||||
import { toast } from "sonner";
|
||||
|
||||
export default function Auth() {
|
||||
const { t } = useLanguage();
|
||||
const { login, register, user } = useAuth();
|
||||
const router = useRouter();
|
||||
const [isLogin, setIsLogin] = useState(true);
|
||||
const [email, setEmail] = useState("");
|
||||
const [password, setPassword] = useState("");
|
||||
const [name, setName] = useState("");
|
||||
const [loading, setLoading] = useState(false);
|
||||
|
||||
const handleSubmit = (e: React.FormEvent) => {
|
||||
useEffect(() => {
|
||||
if (user) router.replace("/");
|
||||
}, [user, router]);
|
||||
|
||||
const handleSubmit = async (e: React.FormEvent) => {
|
||||
e.preventDefault();
|
||||
toast.success(isLogin ? "Connexion réussie" : "Compte créé avec succès !");
|
||||
setLoading(true);
|
||||
try {
|
||||
if (isLogin) {
|
||||
await login(email, password);
|
||||
} else {
|
||||
await register(email, password, name);
|
||||
}
|
||||
toast.success(isLogin ? t("auth.login_success") : t("auth.register_success"));
|
||||
setTimeout(() => router.push("/"), 800);
|
||||
} catch (err) {
|
||||
const msg = err instanceof ApiError ? err.message : t("auth.error");
|
||||
toast.error(msg);
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
@@ -26,36 +50,63 @@ export default function Auth() {
|
||||
{isLogin ? t("auth.login") : t("auth.register")}
|
||||
</h1>
|
||||
<p className="text-sm text-muted-foreground text-center mb-8">
|
||||
{isLogin ? "Accédez à votre espace personnel" : "Créez votre compte en quelques secondes"}
|
||||
{isLogin ? t("auth.login_subtitle") : t("auth.register_subtitle")}
|
||||
</p>
|
||||
|
||||
<form onSubmit={handleSubmit} className="space-y-4">
|
||||
{!isLogin && (
|
||||
<div>
|
||||
<Label htmlFor="name">{t("auth.name")}</Label>
|
||||
<Input id="name" value={name} onChange={(e) => setName(e.target.value)} className="mt-1" />
|
||||
<Input
|
||||
id="name"
|
||||
value={name}
|
||||
onChange={(e) => setName(e.target.value)}
|
||||
className="mt-1"
|
||||
required
|
||||
minLength={2}
|
||||
/>
|
||||
</div>
|
||||
)}
|
||||
<div>
|
||||
<Label htmlFor="email">{t("auth.email")}</Label>
|
||||
<Input id="email" type="email" value={email} onChange={(e) => setEmail(e.target.value)} className="mt-1" />
|
||||
<Input
|
||||
id="email"
|
||||
type="email"
|
||||
value={email}
|
||||
onChange={(e) => setEmail(e.target.value)}
|
||||
className="mt-1"
|
||||
required
|
||||
/>
|
||||
</div>
|
||||
<div>
|
||||
<Label htmlFor="password">{t("auth.password")}</Label>
|
||||
<Input id="password" type="password" value={password} onChange={(e) => setPassword(e.target.value)} className="mt-1" />
|
||||
<Input
|
||||
id="password"
|
||||
type="password"
|
||||
value={password}
|
||||
onChange={(e) => setPassword(e.target.value)}
|
||||
className="mt-1"
|
||||
required
|
||||
minLength={8}
|
||||
/>
|
||||
</div>
|
||||
<Button type="submit" className="w-full" size="lg">
|
||||
{isLogin ? t("auth.login") : t("auth.register")}
|
||||
<Button type="submit" className="w-full" size="lg" disabled={loading}>
|
||||
{loading ? t("auth.loading") : isLogin ? t("auth.login") : t("auth.register")}
|
||||
</Button>
|
||||
</form>
|
||||
|
||||
<p className="text-center text-sm text-muted-foreground mt-6">
|
||||
{isLogin ? "Pas encore de compte ?" : "Déjà un compte ?"}{" "}
|
||||
<Button variant="link" onClick={() => setIsLogin(!isLogin)} className="text-primary hover:underline font-medium">
|
||||
{isLogin ? t("auth.no_account") : t("auth.has_account")}{" "}
|
||||
<Button
|
||||
type="button"
|
||||
variant="link"
|
||||
onClick={() => setIsLogin(!isLogin)}
|
||||
className="text-primary hover:underline font-medium"
|
||||
>
|
||||
{isLogin ? t("auth.register") : t("auth.login")}
|
||||
</Button>
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user