refactor: convert auth register to Server Action
- Add src/app/actions/auth.ts with registerUser - Update RegisterForm to use Server Action - Remove api/auth/register route
This commit is contained in:
@@ -14,6 +14,7 @@
|
|||||||
| `POST /api/komga/libraries/[libraryId]/scan` | `scanLibrary()` | ✅ Done |
|
| `POST /api/komga/libraries/[libraryId]/scan` | `scanLibrary()` | ✅ Done |
|
||||||
| `POST /api/komga/config` | `saveKomgaConfig()` | ✅ Done |
|
| `POST /api/komga/config` | `saveKomgaConfig()` | ✅ Done |
|
||||||
| `PUT /api/user/password` | `changePassword()` | ✅ Done |
|
| `PUT /api/user/password` | `changePassword()` | ✅ Done |
|
||||||
|
| `POST /api/auth/register` | `registerUser()` | ✅ Done |
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
|
|||||||
23
src/app/actions/auth.ts
Normal file
23
src/app/actions/auth.ts
Normal file
@@ -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" };
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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 }
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -9,6 +9,7 @@ import type { AppErrorType } from "@/types/global";
|
|||||||
import { Input } from "@/components/ui/input";
|
import { Input } from "@/components/ui/input";
|
||||||
import { Label } from "@/components/ui/label";
|
import { Label } from "@/components/ui/label";
|
||||||
import { Button } from "@/components/ui/button";
|
import { Button } from "@/components/ui/button";
|
||||||
|
import { registerUser } from "@/app/actions/auth";
|
||||||
|
|
||||||
interface RegisterFormProps {
|
interface RegisterFormProps {
|
||||||
from?: string;
|
from?: string;
|
||||||
@@ -41,24 +42,15 @@ export function RegisterForm({ from: _from }: RegisterFormProps) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// Étape 1: Inscription via l'API
|
// Étape 1: Inscription via Server Action
|
||||||
const response = await fetch("/api/auth/register", {
|
const result = await registerUser(email, password);
|
||||||
method: "POST",
|
|
||||||
headers: {
|
|
||||||
"Content-Type": "application/json",
|
|
||||||
},
|
|
||||||
body: JSON.stringify({ email, password }),
|
|
||||||
});
|
|
||||||
|
|
||||||
if (!response.ok) {
|
if (!result.success) {
|
||||||
const data = await response.json();
|
setError({
|
||||||
setError(
|
|
||||||
data.error || {
|
|
||||||
code: "AUTH_REGISTRATION_FAILED",
|
code: "AUTH_REGISTRATION_FAILED",
|
||||||
name: "Registration failed",
|
name: "Registration failed",
|
||||||
message: "Erreur lors de l'inscription",
|
message: result.message,
|
||||||
}
|
});
|
||||||
);
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user