Files
afrov2/app/actions/auth.ts

68 lines
1.9 KiB
TypeScript

"use server";
import { prisma } from "@/lib/prisma";
import { revalidatePath } from "next/cache";
function escapeHtml(value: string): string {
return value
.replace(/&/g, "&")
.replace(/</g, "&lt;")
.replace(/>/g, "&gt;")
.replace(/"/g, "&quot;")
.replace(/'/g, "&#39;");
}
export async function reactivateUserAccount(userId: string, reason: string) {
try {
if (!userId || typeof reason !== "string" || reason.trim().length === 0) {
return { success: false, error: "Paramètres invalides" };
}
const sanitizedReason = reason.trim().slice(0, 1000);
const user = await prisma.user.findUnique({
where: { id: userId },
select: { id: true, name: true, email: true, deletedAt: true }
});
if (!user) {
return { success: false, error: "Utilisateur non trouvé" };
}
if (!user.deletedAt) {
return { success: false, error: "Ce compte est déjà actif" };
}
await prisma.user.update({
where: { id: userId },
data: {
deletedAt: null,
deletionScheduledAt: null,
reactivationReason: sanitizedReason
}
});
// Optionnel : Envoyer un mail de confirmation
const settings = await prisma.siteSetting.findUnique({ where: { id: "singleton" } });
if (settings?.reactivateAccountTemplate) {
const { sendEmail } = await import("@/lib/mail");
const html = settings.reactivateAccountTemplate
.replace(/{name}/g, escapeHtml(user.name || ""))
.replace(/{siteName}/g, escapeHtml(settings.siteName || "Afrohub"))
.replace(/{reason}/g, escapeHtml(sanitizedReason));
await sendEmail({
to: user.email,
subject: settings.reactivateAccountSubject,
html
});
}
revalidatePath("/");
return { success: true };
} catch (error) {
console.error("Reactivation Error:", error);
return { success: false, error: "Une erreur est survenue lors de la réactivation" };
}
}