refactor: convert password change to Server Action
- Add src/app/actions/password.ts with changePassword - Update ChangePasswordForm to use Server Action - Remove api/user/password route (entire file)
This commit is contained in:
@@ -13,6 +13,7 @@
|
|||||||
| `PUT /api/preferences` | `updatePreferences()` | ✅ Done |
|
| `PUT /api/preferences` | `updatePreferences()` | ✅ Done |
|
||||||
| `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 |
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
|
|||||||
33
src/app/actions/password.ts
Normal file
33
src/app/actions/password.ts
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
"use server";
|
||||||
|
|
||||||
|
import { UserService } from "@/lib/services/user.service";
|
||||||
|
import { AuthServerService } from "@/lib/services/auth-server.service";
|
||||||
|
import { ERROR_CODES } from "@/constants/errorCodes";
|
||||||
|
import { AppError } from "@/utils/errors";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Change le mot de passe de l'utilisateur
|
||||||
|
*/
|
||||||
|
export async function changePassword(
|
||||||
|
currentPassword: string,
|
||||||
|
newPassword: string
|
||||||
|
): Promise<{ success: boolean; message: string }> {
|
||||||
|
try {
|
||||||
|
// Vérifier que le nouveau mot de passe est fort
|
||||||
|
if (!AuthServerService.isPasswordStrong(newPassword)) {
|
||||||
|
return {
|
||||||
|
success: false,
|
||||||
|
message: "Le nouveau mot de passe doit contenir au moins 8 caractères, une majuscule et un chiffre",
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
await UserService.changePassword(currentPassword, newPassword);
|
||||||
|
|
||||||
|
return { success: true, message: "Mot de passe modifié avec succès" };
|
||||||
|
} catch (error) {
|
||||||
|
if (error instanceof AppError) {
|
||||||
|
return { success: false, message: error.message };
|
||||||
|
}
|
||||||
|
return { success: false, message: "Erreur lors du changement de mot de passe" };
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,52 +0,0 @@
|
|||||||
import { NextRequest, NextResponse } from "next/server";
|
|
||||||
import { UserService } from "@/lib/services/user.service";
|
|
||||||
import { AppError } from "@/utils/errors";
|
|
||||||
import { AuthServerService } from "@/lib/services/auth-server.service";
|
|
||||||
import logger from "@/lib/logger";
|
|
||||||
|
|
||||||
export async function PUT(request: NextRequest) {
|
|
||||||
try {
|
|
||||||
const body = await request.json();
|
|
||||||
const { currentPassword, newPassword } = body;
|
|
||||||
|
|
||||||
if (!currentPassword || !newPassword) {
|
|
||||||
return NextResponse.json({ error: "Mots de passe manquants" }, { status: 400 });
|
|
||||||
}
|
|
||||||
|
|
||||||
// Vérifier que le nouveau mot de passe est fort
|
|
||||||
if (!AuthServerService.isPasswordStrong(newPassword)) {
|
|
||||||
return NextResponse.json(
|
|
||||||
{
|
|
||||||
error:
|
|
||||||
"Le nouveau mot de passe doit contenir au moins 8 caractères, une majuscule et un chiffre",
|
|
||||||
},
|
|
||||||
{ status: 400 }
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
await UserService.changePassword(currentPassword, newPassword);
|
|
||||||
|
|
||||||
return NextResponse.json({ success: true });
|
|
||||||
} catch (error) {
|
|
||||||
logger.error({ err: error }, "Erreur lors du changement de mot de passe:");
|
|
||||||
|
|
||||||
if (error instanceof AppError) {
|
|
||||||
return NextResponse.json(
|
|
||||||
{ error: error.message, code: error.code },
|
|
||||||
{
|
|
||||||
status:
|
|
||||||
error.code === "AUTH_INVALID_PASSWORD"
|
|
||||||
? 400
|
|
||||||
: error.code === "AUTH_UNAUTHENTICATED"
|
|
||||||
? 401
|
|
||||||
: 500,
|
|
||||||
}
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
return NextResponse.json(
|
|
||||||
{ error: "Erreur lors du changement de mot de passe" },
|
|
||||||
{ status: 500 }
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -7,6 +7,7 @@ import { Input } from "@/components/ui/input";
|
|||||||
import { Label } from "@/components/ui/label";
|
import { Label } from "@/components/ui/label";
|
||||||
import { useToast } from "@/components/ui/use-toast";
|
import { useToast } from "@/components/ui/use-toast";
|
||||||
import { Lock } from "lucide-react";
|
import { Lock } from "lucide-react";
|
||||||
|
import { changePassword } from "@/app/actions/password";
|
||||||
|
|
||||||
export function ChangePasswordForm() {
|
export function ChangePasswordForm() {
|
||||||
const [currentPassword, setCurrentPassword] = useState("");
|
const [currentPassword, setCurrentPassword] = useState("");
|
||||||
@@ -39,15 +40,10 @@ export function ChangePasswordForm() {
|
|||||||
setIsLoading(true);
|
setIsLoading(true);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const response = await fetch("/api/user/password", {
|
const result = await changePassword(currentPassword, newPassword);
|
||||||
method: "PUT",
|
|
||||||
headers: { "Content-Type": "application/json" },
|
|
||||||
body: JSON.stringify({ currentPassword, newPassword }),
|
|
||||||
});
|
|
||||||
|
|
||||||
if (!response.ok) {
|
if (!result.success) {
|
||||||
const data = await response.json();
|
throw new Error(result.message);
|
||||||
throw new Error(data.error || "Erreur lors du changement de mot de passe");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
toast({
|
toast({
|
||||||
|
|||||||
Reference in New Issue
Block a user