|
| 1 | +import { sendResponse } from '../../utils/response.js'; |
| 2 | +import nodemailer from 'nodemailer'; |
| 3 | +import crypto from 'crypto'; |
| 4 | + |
| 5 | +const forgotPassword = async (req, res) => { |
| 6 | + try { |
| 7 | + const { email } = req.body; |
| 8 | + |
| 9 | + // Validate email |
| 10 | + if (!email) { |
| 11 | + return sendResponse(res, 400, 'Email is required'); |
| 12 | + } |
| 13 | + |
| 14 | + // TODO: Check if user exists in database |
| 15 | + // const user = await User.findOne({ email }); |
| 16 | + // if (!user) { |
| 17 | + // return sendResponse(res, 404, 'User not found'); |
| 18 | + // } |
| 19 | + |
| 20 | + // Generate reset token |
| 21 | + const resetToken = crypto.randomBytes(32).toString('hex'); |
| 22 | + const resetTokenExpiry = Date.now() + 3600000; // 1 hour |
| 23 | + |
| 24 | + // TODO: Save token to database |
| 25 | + // user.passwordResetToken = resetToken; |
| 26 | + // user.passwordResetExpires = resetTokenExpiry; |
| 27 | + // await user.save(); |
| 28 | + |
| 29 | + // Create reset URL |
| 30 | + const resetUrl = `${process.env.FRONTEND_URL || 'http://localhost:5173'}/reset-password/${resetToken}`; |
| 31 | + |
| 32 | + // Configure email transporter |
| 33 | + const transporter = nodemailer.createTransport({ |
| 34 | + host: process.env.EMAIL_HOST || 'smtp.gmail.com', |
| 35 | + port: process.env.EMAIL_PORT || 587, |
| 36 | + secure: false, |
| 37 | + auth: { |
| 38 | + user: process.env.EMAIL_USER, |
| 39 | + pass: process.env.EMAIL_PASSWORD, |
| 40 | + }, |
| 41 | + }); |
| 42 | + |
| 43 | + // Email content |
| 44 | + const mailOptions = { |
| 45 | + from: `"Code A2Z" <${process.env.EMAIL_USER}>`, |
| 46 | + to: email, |
| 47 | + subject: 'Password Reset Request', |
| 48 | + html: ` |
| 49 | + <div style="font-family: Arial, sans-serif; padding: 20px;"> |
| 50 | + <h2>Password Reset Request</h2> |
| 51 | + <p>You requested a password reset for your Code A2Z account.</p> |
| 52 | + <p>Click the link below to reset your password:</p> |
| 53 | + <a href="${resetUrl}" style="display: inline-block; padding: 10px 20px; background-color: #4CAF50; color: white; text-decoration: none; border-radius: 5px;">Reset Password</a> |
| 54 | + <p style="margin-top: 20px;">This link will expire in 1 hour.</p> |
| 55 | + <p>If you didn't request this, please ignore this email.</p> |
| 56 | + </div> |
| 57 | + `, |
| 58 | + }; |
| 59 | + |
| 60 | + // Send email |
| 61 | + await transporter.sendMail(mailOptions); |
| 62 | + |
| 63 | + console.log('Password reset email sent to:', email); |
| 64 | + |
| 65 | + return sendResponse( |
| 66 | + res, |
| 67 | + 200, |
| 68 | + 'Password reset link sent successfully to your email' |
| 69 | + ); |
| 70 | + } catch (err) { |
| 71 | + console.error('Forgot password error:', err); |
| 72 | + return sendResponse( |
| 73 | + res, |
| 74 | + 500, |
| 75 | + err.message || 'Failed to send reset link. Please try again later.' |
| 76 | + ); |
| 77 | + } |
| 78 | +}; |
| 79 | + |
| 80 | +export default forgotPassword; |
0 commit comments