diff --git a/docs/server-actions-plan.md b/docs/server-actions-plan.md index 2082d69..8797af8 100644 --- a/docs/server-actions-plan.md +++ b/docs/server-actions-plan.md @@ -14,6 +14,7 @@ | `POST /api/komga/libraries/[libraryId]/scan` | `scanLibrary()` | ✅ Done | | `POST /api/komga/config` | `saveKomgaConfig()` | ✅ Done | | `PUT /api/user/password` | `changePassword()` | ✅ Done | +| `POST /api/auth/register` | `registerUser()` | ✅ Done | --- diff --git a/src/app/actions/auth.ts b/src/app/actions/auth.ts new file mode 100644 index 0000000..f1581cb --- /dev/null +++ b/src/app/actions/auth.ts @@ -0,0 +1,23 @@ +"use server"; + +import { AuthServerService } from "@/lib/services/auth-server.service"; +import { ERROR_CODES } from "@/constants/errorCodes"; +import { AppError } from "@/utils/errors"; + +/** + * Inscrit un nouvel utilisateur + */ +export async function registerUser( + email: string, + password: string +): Promise<{ success: boolean; message: string }> { + try { + await AuthServerService.registerUser(email, password); + return { success: true, message: "Inscription réussie" }; + } catch (error) { + if (error instanceof AppError) { + return { success: false, message: error.message }; + } + return { success: false, message: "Erreur lors de l'inscription" }; + } +} diff --git a/src/app/api/auth/register/route.ts b/src/app/api/auth/register/route.ts deleted file mode 100644 index eae0e00..0000000 --- a/src/app/api/auth/register/route.ts +++ /dev/null @@ -1,55 +0,0 @@ -import { NextRequest, NextResponse } from "next/server"; -import { AuthServerService } from "@/lib/services/auth-server.service"; -import { ERROR_CODES } from "@/constants/errorCodes"; -import { ERROR_MESSAGES } from "@/constants/errorMessages"; -import { AppError } from "@/utils/errors"; -import logger from "@/lib/logger"; - -export async function POST(request: NextRequest) { - try { - const { email, password } = await request.json(); - - if (!email || !password) { - return NextResponse.json( - { - error: { - code: ERROR_CODES.AUTH.INVALID_USER_DATA, - name: "Invalid user data", - message: ERROR_MESSAGES[ERROR_CODES.AUTH.INVALID_USER_DATA], - } as AppError, - }, - { status: 400 } - ); - } - - const userData = await AuthServerService.registerUser(email, password); - - return NextResponse.json({ success: true, user: userData }); - } catch (error) { - logger.error({ err: error }, "Registration error:"); - - if (error instanceof AppError) { - return NextResponse.json( - { - error: { - code: error.code, - name: error.name, - message: error.message, - } as AppError, - }, - { status: 400 } - ); - } - - return NextResponse.json( - { - error: { - code: ERROR_CODES.AUTH.REGISTRATION_FAILED, - name: "Registration failed", - message: ERROR_MESSAGES[ERROR_CODES.AUTH.REGISTRATION_FAILED], - } as AppError, - }, - { status: 500 } - ); - } -} diff --git a/src/components/auth/RegisterForm.tsx b/src/components/auth/RegisterForm.tsx index c2489d2..9f18ecd 100644 --- a/src/components/auth/RegisterForm.tsx +++ b/src/components/auth/RegisterForm.tsx @@ -9,6 +9,7 @@ import type { AppErrorType } from "@/types/global"; import { Input } from "@/components/ui/input"; import { Label } from "@/components/ui/label"; import { Button } from "@/components/ui/button"; +import { registerUser } from "@/app/actions/auth"; interface RegisterFormProps { from?: string; @@ -41,24 +42,15 @@ export function RegisterForm({ from: _from }: RegisterFormProps) { } try { - // É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 }), - }); + // Étape 1: Inscription via Server Action + const result = await registerUser(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", - } - ); + if (!result.success) { + setError({ + code: "AUTH_REGISTRATION_FAILED", + name: "Registration failed", + message: result.message, + }); return; }