title: user email verification using nodemailer
body: <p>I am doing the user email verification in node js using nodemailer my code is below.</p>
  <pre><code>// Create new User
  const createUser = async function (req, res) {
  
      // Check if this user already exisits.
      let user = await User.findOne({ email: req.body.email });
      if (user) {
          return res.status(400).send('That user already exists!');
      } else {
          // Insert the new user if they do not exist yet.
          user = new User({
              name: req.body.name,
              email: req.body.email,
              password: req.body.password
          });
  
          // Hash the password before saving into database.
          const salt = await bcrypt.genSalt(10);
          user.password = await bcrypt.hash(user.password, salt);
          await user.save();
  
          // Generating the token for user verification
          const token = new Token({ userId: user._id, token: crypto.randomBytes(16).toString('hex') });
          await token.save();
  
          // Send varification email
          const link = `${process.env.BASE_URL}/users/confirm/${token.token}`;
          await sendEmail(user.email, &quot;Email Verification\n&quot;, link);
          console.log(token)
          res.status(200).send({
              message: &quot;Email Verification link sent to your email&quot;,
          });
      }
  };
  
  // Verify Email address Api
  const confirmationPost = function (req, res, next) {
  
      // Find a matching token
      Token.findOne({ token: req.params.token }, function (err, token) {
          if (!token) return res.status(400).send({
              type: 'not-verified',
              msg: `We were unable to find a valid token.Your token my have expired.`
          });
  
          // If we found a token, find a matching user
          User.findOne({ _id: token.userId }, function (err, user) {
              if (!user) return res.status(400).send({ msg: 'We were unable to find a user for this token.' });
              if (user.isVerified) return res.status(400).send({
                  type: 'already-verified',
                  msg: 'This user has already been verified.'
              });
  
              // Verify and save the user
              user.isVerified = true;
              user.save(function (err) {
                  if (err) { return res.status(500).send({ msg: err.message }); }
                  res.status(200).send(&quot;The account has been verified. Please login.&quot;);
              });
          });
      });
  };
  </code></pre>
  <p>// Routes</p>
  <p>router.post('/api/users/create', createUser);</p>
  <p>router.get('/api/users/confirm/:token', confirmationPost);</p>
  <p>// This is sendEmail file</p>
  <pre><code>const nodemailer = require('nodemailer');
  
  const sendEmail = async (email, subject, link) =&gt; {
      try {
          const transporter = nodemailer.createTransport({
              host: &quot;smtp.mailtrap.io&quot;,
              port: 2525,
              auth: {
                  user: process.env.USER,
                  pass: process.env.PASS,
              },`enter code here`
          });
  
          await transporter.sendMail({
              from: 'noreply@gmail.com',
              to: email,
              subject: subject,
              text: 'Hello,\n Welcome. Please click on the link to verify your account.\n' +link,
              html: '&lt;a href=&quot;${link}&quot;&gt;Verify Email.&lt;/a&gt;'
          });
  
      } catch (error) {
          return error;
      }
  };
  
  module.exports = sendEmail;
  </code></pre>
  <p>This code worked and I get an email on my mail trap login but when I click on verify email it does not work. The email I got.</p>
  <p>Hello,
  Welcome. Please click on the link to verify your account.
  http://localhost:5000/api/users/confirm/10da46424f275844cad3a2635e05546d</p>
  <p>In the text version of the email, I am getting the correct link but when I click on the verify link it said no URL exists. I think maybe there is some syntax problem with that line can someone guide me?</p>
  <p>html: 'Verify Email.'</p>