Maj smtp, reset password, et ajout afroshine
All checks were successful
Build and Push App / build (push) Successful in 10m29s
All checks were successful
Build and Push App / build (push) Successful in 10m29s
This commit is contained in:
60
app/api/auth/forgot-password/route.ts
Normal file
60
app/api/auth/forgot-password/route.ts
Normal file
@@ -0,0 +1,60 @@
|
||||
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 });
|
||||
}
|
||||
}
|
||||
42
app/api/auth/reset-password/route.ts
Normal file
42
app/api/auth/reset-password/route.ts
Normal file
@@ -0,0 +1,42 @@
|
||||
import { NextResponse } from 'next/server';
|
||||
import prisma from '@/lib/prisma';
|
||||
import bcrypt from 'bcryptjs';
|
||||
|
||||
export async function POST(req: Request) {
|
||||
try {
|
||||
const { token, password } = await req.json();
|
||||
|
||||
if (!token || !password) {
|
||||
return NextResponse.json({ error: 'Token et mot de passe sont requis' }, { status: 400 });
|
||||
}
|
||||
|
||||
const user = await prisma.user.findFirst({
|
||||
where: {
|
||||
resetToken: token,
|
||||
resetTokenExpiry: {
|
||||
gt: new Date(),
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
if (!user) {
|
||||
return NextResponse.json({ error: 'Token invalide ou expiré' }, { status: 400 });
|
||||
}
|
||||
|
||||
const hashedPassword = await bcrypt.hash(password, 10);
|
||||
|
||||
await prisma.user.update({
|
||||
where: { id: user.id },
|
||||
data: {
|
||||
password: hashedPassword,
|
||||
resetToken: null,
|
||||
resetTokenExpiry: null,
|
||||
},
|
||||
});
|
||||
|
||||
return NextResponse.json({ message: 'Votre mot de passe a été mis à jour avec succès' });
|
||||
} catch (error: any) {
|
||||
console.error('Error in reset-password:', error);
|
||||
return NextResponse.json({ error: 'Une erreur est survenue lors de la réinitialisation' }, { status: 500 });
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user