41 lines
1.2 KiB
TypeScript
41 lines
1.2 KiB
TypeScript
import { NextResponse } from "next/server";
|
|
import { auth } from "@/lib/auth";
|
|
import { userService } from "@/services/users/user.service";
|
|
import { ValidationError, NotFoundError } from "@/services/errors";
|
|
|
|
export async function PUT(request: Request) {
|
|
try {
|
|
const session = await auth();
|
|
|
|
if (!session?.user) {
|
|
return NextResponse.json({ error: "Non authentifié" }, { status: 401 });
|
|
}
|
|
|
|
const body = await request.json();
|
|
const { currentPassword, newPassword, confirmPassword } = body;
|
|
|
|
await userService.validateAndUpdatePassword(
|
|
session.user.id,
|
|
currentPassword,
|
|
newPassword,
|
|
confirmPassword
|
|
);
|
|
|
|
return NextResponse.json({ message: "Mot de passe modifié avec succès" });
|
|
} catch (error) {
|
|
console.error("Error updating password:", error);
|
|
|
|
if (error instanceof ValidationError) {
|
|
return NextResponse.json({ error: error.message }, { status: 400 });
|
|
}
|
|
if (error instanceof NotFoundError) {
|
|
return NextResponse.json({ error: error.message }, { status: 404 });
|
|
}
|
|
|
|
return NextResponse.json(
|
|
{ error: "Erreur lors de la modification du mot de passe" },
|
|
{ status: 500 }
|
|
);
|
|
}
|
|
}
|