if user has pass ask for it before deleting it
This commit is contained in:
@@ -168,20 +168,39 @@ export async function deleteUser(req: FastifyRequest, reply: FastifyReply) {
|
||||
const { id } = req.params as { id: string };
|
||||
const userId = parseInt(id, 10);
|
||||
|
||||
const body = (req.body ?? {}) as { password?: string };
|
||||
const password = body.password;
|
||||
|
||||
if (!userId || isNaN(userId)) {
|
||||
return reply.code(400).send({ error: "Invalid user id" });
|
||||
}
|
||||
|
||||
const result = await userService.deleteUser(userId);
|
||||
|
||||
if (result && result.changes > 0) {
|
||||
return { success: true, message: "User deleted successfully" };
|
||||
} else {
|
||||
const user = await userService.getUserById(userId);
|
||||
if (!user) {
|
||||
return reply.code(404).send({ error: "User not found" });
|
||||
}
|
||||
|
||||
if (user.has_password) {
|
||||
if (!password) {
|
||||
return reply.code(401).send({ error: "Password required" });
|
||||
}
|
||||
|
||||
const isValid = await userService.verifyPassword(userId, password);
|
||||
if (!isValid) {
|
||||
return reply.code(401).send({ error: "Incorrect password" });
|
||||
}
|
||||
}
|
||||
|
||||
const result = await userService.deleteUser(userId);
|
||||
|
||||
if (result.changes > 0) {
|
||||
return reply.send({ success: true });
|
||||
}
|
||||
|
||||
return reply.code(500).send({ error: "Failed to delete user" });
|
||||
|
||||
} catch (err) {
|
||||
console.error("Delete User Error:", (err as Error).message);
|
||||
console.error("Delete User Error:", err);
|
||||
return reply.code(500).send({ error: "Failed to delete user" });
|
||||
}
|
||||
}
|
||||
@@ -246,16 +265,17 @@ export async function changePassword(req: FastifyRequest, reply: FastifyReply) {
|
||||
return reply.code(404).send({ error: "User not found" });
|
||||
}
|
||||
|
||||
// Si el usuario tiene contraseña actual, debe proporcionar la contraseña actual
|
||||
if (user.has_password && currentPassword) {
|
||||
const isValid = await userService.verifyPassword(userId, currentPassword);
|
||||
if (user.has_password) {
|
||||
if (!currentPassword) {
|
||||
return reply.code(401).send({ error: "Current password required" });
|
||||
}
|
||||
|
||||
const isValid = await userService.verifyPassword(userId, currentPassword);
|
||||
if (!isValid) {
|
||||
return reply.code(401).send({ error: "Current password is incorrect" });
|
||||
}
|
||||
}
|
||||
|
||||
// Actualizar la contraseña (null para eliminarla, string para establecerla)
|
||||
await userService.updateUser(userId, { password: newPassword });
|
||||
|
||||
return reply.send({
|
||||
|
||||
Reference in New Issue
Block a user