feat: integrate NextAuth for authentication, refactor login and registration processes, and enhance middleware for session management
This commit is contained in:
@@ -2,9 +2,7 @@
|
||||
|
||||
import { useState } from "react";
|
||||
import { useRouter } from "next/navigation";
|
||||
import { authService } from "@/lib/services/auth.service";
|
||||
import type { AppErrorType } from "@/types/global";
|
||||
import { ErrorMessage } from "@/components/ui/ErrorMessage";
|
||||
import { signIn } from "next-auth/react";
|
||||
import { useTranslate } from "@/hooks/useTranslate";
|
||||
|
||||
interface LoginFormProps {
|
||||
@@ -14,7 +12,7 @@ interface LoginFormProps {
|
||||
export function LoginForm({ from }: LoginFormProps) {
|
||||
const router = useRouter();
|
||||
const [isLoading, setIsLoading] = useState(false);
|
||||
const [error, setError] = useState<AppErrorType | null>(null);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
const { t } = useTranslate();
|
||||
|
||||
const handleSubmit = async (event: React.FormEvent<HTMLFormElement>) => {
|
||||
@@ -28,11 +26,21 @@ export function LoginForm({ from }: LoginFormProps) {
|
||||
const remember = formData.get("remember") === "on";
|
||||
|
||||
try {
|
||||
await authService.login(email, password, remember);
|
||||
router.push(from || "/");
|
||||
router.refresh();
|
||||
} catch (error) {
|
||||
setError(error as AppErrorType);
|
||||
const result = await signIn("credentials", {
|
||||
email,
|
||||
password,
|
||||
remember,
|
||||
redirect: false,
|
||||
});
|
||||
|
||||
if (result?.error) {
|
||||
setError("Email ou mot de passe incorrect");
|
||||
} else {
|
||||
router.push(from || "/");
|
||||
router.refresh();
|
||||
}
|
||||
} catch (_error) {
|
||||
setError("Une erreur est survenue lors de la connexion : " + _error);
|
||||
} finally {
|
||||
setIsLoading(false);
|
||||
}
|
||||
@@ -89,7 +97,11 @@ export function LoginForm({ from }: LoginFormProps) {
|
||||
{t("login.form.remember")}
|
||||
</label>
|
||||
</div>
|
||||
{error && <ErrorMessage errorCode={error.code} variant="form" />}
|
||||
{error && (
|
||||
<div className="text-red-600 text-sm">
|
||||
{error}
|
||||
</div>
|
||||
)}
|
||||
<button
|
||||
type="submit"
|
||||
disabled={isLoading}
|
||||
|
||||
@@ -2,18 +2,16 @@
|
||||
|
||||
import { useState } from "react";
|
||||
import { useRouter } from "next/navigation";
|
||||
import { authService } from "@/lib/services/auth.service";
|
||||
import type { AppErrorType } from "@/types/global";
|
||||
import { ERROR_CODES } from "@/constants/errorCodes";
|
||||
import { signIn } from "next-auth/react";
|
||||
import { ErrorMessage } from "@/components/ui/ErrorMessage";
|
||||
import { useTranslate } from "@/hooks/useTranslate";
|
||||
import { getErrorMessage } from "@/utils/errors";
|
||||
import type { AppErrorType } from "@/types/global";
|
||||
|
||||
interface RegisterFormProps {
|
||||
from?: string;
|
||||
}
|
||||
|
||||
export function RegisterForm({ from }: RegisterFormProps) {
|
||||
export function RegisterForm({ from: _from }: RegisterFormProps) {
|
||||
const router = useRouter();
|
||||
const [isLoading, setIsLoading] = useState(false);
|
||||
const [error, setError] = useState<AppErrorType | null>(null);
|
||||
@@ -31,20 +29,57 @@ export function RegisterForm({ from }: RegisterFormProps) {
|
||||
|
||||
if (password !== confirmPassword) {
|
||||
setError({
|
||||
code: ERROR_CODES.AUTH.PASSWORD_MISMATCH,
|
||||
code: "AUTH_PASSWORD_MISMATCH",
|
||||
name: "Password mismatch",
|
||||
message: getErrorMessage(ERROR_CODES.AUTH.PASSWORD_MISMATCH),
|
||||
message: "Les mots de passe ne correspondent pas",
|
||||
});
|
||||
setIsLoading(false);
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
await authService.register(email, password);
|
||||
router.push(from || "/");
|
||||
router.refresh();
|
||||
} catch (error) {
|
||||
setError(error as AppErrorType);
|
||||
// Étape 1: Inscription via l'API
|
||||
const response = await fetch("/api/auth/register", {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
body: JSON.stringify({ email, password }),
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
const data = await response.json();
|
||||
setError(data.error || {
|
||||
code: "AUTH_REGISTRATION_FAILED",
|
||||
name: "Registration failed",
|
||||
message: "Erreur lors de l'inscription",
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
// Étape 2: Connexion automatique via NextAuth
|
||||
const signInResult = await signIn("credentials", {
|
||||
email,
|
||||
password,
|
||||
redirect: false,
|
||||
});
|
||||
|
||||
if (signInResult?.error) {
|
||||
setError({
|
||||
code: "AUTH_INVALID_CREDENTIALS",
|
||||
name: "Login failed",
|
||||
message: "Inscription réussie mais erreur lors de la connexion automatique",
|
||||
});
|
||||
} else {
|
||||
router.push("/");
|
||||
router.refresh();
|
||||
}
|
||||
} catch {
|
||||
setError({
|
||||
code: "AUTH_REGISTRATION_FAILED",
|
||||
name: "Registration failed",
|
||||
message: "Une erreur est survenue lors de l'inscription",
|
||||
});
|
||||
} finally {
|
||||
setIsLoading(false);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user