94 lines
2.4 KiB
TypeScript
94 lines
2.4 KiB
TypeScript
import { NextRequest, NextResponse } from 'next/server';
|
|
import { prisma } from '@/lib/prisma';
|
|
import { getCurrentUser } from '@/lib/auth';
|
|
import bcrypt from 'bcryptjs';
|
|
|
|
// DELETE - Supprimer un utilisateur
|
|
export async function DELETE(
|
|
request: NextRequest,
|
|
{ params }: { params: { id: string } }
|
|
) {
|
|
try {
|
|
const currentUser = await getCurrentUser();
|
|
if (!currentUser) {
|
|
return NextResponse.json({ error: 'Non autorisé' }, { status: 401 });
|
|
}
|
|
|
|
// Empêcher la suppression de son propre compte
|
|
if (currentUser.id === params.id) {
|
|
return NextResponse.json(
|
|
{ error: 'Vous ne pouvez pas supprimer votre propre compte' },
|
|
{ status: 400 }
|
|
);
|
|
}
|
|
|
|
const user = await prisma.user.findUnique({
|
|
where: { id: params.id },
|
|
});
|
|
|
|
if (!user) {
|
|
return NextResponse.json(
|
|
{ error: 'Utilisateur non trouvé' },
|
|
{ status: 404 }
|
|
);
|
|
}
|
|
|
|
await prisma.user.delete({
|
|
where: { id: params.id },
|
|
});
|
|
|
|
return NextResponse.json({ success: true });
|
|
} catch (error) {
|
|
console.error('Erreur lors de la suppression de l\'utilisateur:', error);
|
|
return NextResponse.json(
|
|
{ error: 'Erreur serveur' },
|
|
{ status: 500 }
|
|
);
|
|
}
|
|
}
|
|
|
|
// PUT - Réinitialiser le mot de passe d'un utilisateur
|
|
export async function PUT(
|
|
request: NextRequest,
|
|
{ params }: { params: { id: string } }
|
|
) {
|
|
try {
|
|
const currentUser = await getCurrentUser();
|
|
if (!currentUser) {
|
|
return NextResponse.json({ error: 'Non autorisé' }, { status: 401 });
|
|
}
|
|
|
|
const body = await request.json();
|
|
const { action } = body;
|
|
|
|
if (action === 'reset-password') {
|
|
// Générer un nouveau mot de passe aléatoire
|
|
const newPassword = Math.random().toString(36).slice(-12) + Math.random().toString(36).slice(-12);
|
|
const hashedPassword = await bcrypt.hash(newPassword, 10);
|
|
|
|
await prisma.user.update({
|
|
where: { id: params.id },
|
|
data: {
|
|
password: hashedPassword,
|
|
},
|
|
});
|
|
|
|
return NextResponse.json({
|
|
success: true,
|
|
newPassword, // Retourner le mot de passe en clair pour l'affichage
|
|
});
|
|
}
|
|
|
|
return NextResponse.json(
|
|
{ error: 'Action non reconnue' },
|
|
{ status: 400 }
|
|
);
|
|
} catch (error) {
|
|
console.error('Erreur lors de la réinitialisation du mot de passe:', error);
|
|
return NextResponse.json(
|
|
{ error: 'Erreur serveur' },
|
|
{ status: 500 }
|
|
);
|
|
}
|
|
}
|