61 lines
2.5 KiB
TypeScript
61 lines
2.5 KiB
TypeScript
import { NextResponse } from 'next/server';
|
|
import prisma from '@/lib/prisma';
|
|
import { sendEmail } from '@/lib/mail';
|
|
import crypto from 'crypto';
|
|
|
|
export async function POST(req: Request) {
|
|
try {
|
|
const { email } = await req.json();
|
|
|
|
if (!email) {
|
|
return NextResponse.json({ error: 'Email est requis' }, { status: 400 });
|
|
}
|
|
|
|
const user = await prisma.user.findUnique({
|
|
where: { email },
|
|
});
|
|
|
|
if (!user) {
|
|
// Pour des raisons de sécurité, on ne dit pas si l'utilisateur existe ou non
|
|
return NextResponse.json({ message: 'Si un compte existe pour cet email, un lien de réinitialisation a été envoyé.' });
|
|
}
|
|
|
|
// Générer un token
|
|
const token = crypto.randomBytes(32).toString('hex');
|
|
const expiry = new Date(Date.now() + 3600000); // 1 heure
|
|
|
|
await prisma.user.update({
|
|
where: { id: user.id },
|
|
data: {
|
|
resetToken: token,
|
|
resetTokenExpiry: expiry,
|
|
},
|
|
});
|
|
|
|
const resetUrl = `${process.env.NEXT_PUBLIC_APP_URL || 'http://localhost:3000'}/reset-password?token=${token}`;
|
|
|
|
await sendEmail({
|
|
to: email,
|
|
subject: 'Réinitialisation de votre mot de passe - Afrohub',
|
|
html: `
|
|
<div style="font-family: sans-serif; max-width: 600px; margin: 0 auto; padding: 20px; border: 1px solid #e5e7eb; rounded-lg">
|
|
<h2 style="color: #0d9488; text-align: center;">Afrohub</h2>
|
|
<p>Bonjour ${user.name},</p>
|
|
<p>Vous avez demandé la réinitialisation de votre mot de passe. Cliquez sur le bouton ci-dessous pour choisir un nouveau mot de passe :</p>
|
|
<div style="text-align: center; margin: 30px 0;">
|
|
<a href="${resetUrl}" style="background-color: #0d9488; color: white; padding: 12px 24px; text-decoration: none; border-radius: 6px; font-weight: bold;">Réinitialiser mon mot de passe</a>
|
|
</div>
|
|
<p>Ce lien expirera dans 1 heure. Si vous n'avez pas demandé cette réinitialisation, vous pouvez ignorer cet email.</p>
|
|
<hr style="border: 0; border-top: 1px solid #e5e7eb; margin: 20px 0;">
|
|
<p style="font-size: 12px; color: #6b7280; text-align: center;">© 2025 Afrohub. Tous droits réservés.</p>
|
|
</div>
|
|
`,
|
|
});
|
|
|
|
return NextResponse.json({ message: 'Si un compte existe pour cet email, un lien de réinitialisation a été envoyé.' });
|
|
} catch (error: any) {
|
|
console.error('Error in forgot-password:', error);
|
|
return NextResponse.json({ error: 'Une erreur est survenue lors de la demande' }, { status: 500 });
|
|
}
|
|
}
|