How to Create steps for forgot password link in node js ?

Here, we will write a program to create forgot password link. We will also create a reset password page where the user can set the new password. We will use node mailer to send the reset password link to the user.

app.js

const express = require('express');
const bodyParser = require('body-parser');
const nodemailer = require('nodemailer');
const ejs = require('ejs');

const app = express();
app.set("view engine","ejs")

app.use(bodyParser.urlencoded({ extended: false }));

// Render the forgot password form
app.get('/forgot-password', (req, res) => {
  res.render('forgot-password.ejs');
});

// Handle the forgot password form submission
app.post('/forgot-password', (req, res) => {
  const email = req.body.email;

  // Generate a unique token for this password reset request
  const token = Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15);

  // Create a nodemailer transporter
  const transporter = nodemailer.createTransport({
    service: 'Gmail',
    auth: {
      user: 'vsitdwarka@gmail.com',
      pass: 'ncxfvvmekdggosfm'
    }
  });

  // Send the password reset email
  const mailOptions = {
    to: email,
    subject: 'Password Reset Request',
    text: `Click the following link to reset your password: http://localhost:3000/reset-password/${token}`,
    html: `<p>Click the following link to reset your password:</p><p><a href="http://localhost:3000/reset-password/${token}">http://localhost:3000/reset-password/${token}</a></p>`
  };

  transporter.sendMail(mailOptions, (err, info) => {
    if (err) {
      console.log(err);
      res.send('Error sending email');
    } else {
      console.log(info);
      res.send('Password reset email sent');
    }
  });
});

// Render the reset password form
app.get('/reset-password/:token', (req, res) => {
  const token = req.params.token;

  res.render('reset-password.ejs', { token });
});

// Handle the reset password form submission
app.post('/reset-password/:token', (req, res) => {
  const token = req.params.token;
  const password = req.body.password;
  const confirm_password = req.body.confirm_password;

  // Check if passwords match
  if (password !== confirm_password) {
    res.render('reset-password.ejs', { token, message: 'Passwords do not match' });
    return;
  }

  res.send('Password reset successful');
});


app.listen(3000, () => {
  console.log('Server listening on port 3000');
});

forgot-password.ejs file in views directory

<!DOCTYPE html>
<html>
  <head>
    <title>Forgot Password</title>
  </head>
  <body>
    <h1>Forgot Password</h1>
    <form method="POST" action="/forgot-password">
      <label for="email">Email:</label>
      <input type="email" name="email" id="email" required>
      <button type="submit">Submit</button>
    </form>
  </body>
</html>

Reset-password.ejs file in views directory

<!DOCTYPE html>
<html>
  <head>
    <title>Reset Password</title>
  </head>
  <body>
    <h1>Reset Password</h1>

    <form method="POST" action="/reset-password/<%= token %>">
      <label for="password">New Password:</label>
      <input type="password" name="password" id="password" required>
      <label for="confirm_password">Confirm New Password:</label>
      <input type="password" name="confirm_password" id="confirm_password" required>
      <button type="submit">Submit</button>
    </form>
  </body>
</html>