-1

I'm new to website building. I am using node js, express, and express-handlebars. I have 3 hbs page file called signup, verify, and login. I am trying to check if signup page has any errors using exports.signup and then if it's alright then rendering verify page and authenticating it using otp. Now my problem is I need to enter signup page values from verify page in the database after user is verified. How can I get signup page values from exports.signup and use it in exports.verify function?

This works to check in signup page:

exports.signup = (req, res) => { console.log(req.body); 
const { name, email, password, passwordConfirm } = req.body;
  db.query("select email from test where email=?",[email],async (error, results) => {
      if (error) {
        console.log(error);
      }
      if (results.length > 0) {
        return res.render("signup", {
          message: "The email is already in use",
        });
      } else if (password !== passwordConfirm) {
        return res.render("signup", {
          message: "Passwords do not match",
        });
      }
      let hashedPassword = await bcrypt.hash(password, 8);
      console.log(hashedPassword);
      var digits = "0123456789";
      let OTP = "";
      for (let i = 0; i < 6; i++) {
        OTP += digits[Math.floor(Math.random() * 10)];
      }
      let transporter = nodemailer.createTransport({
        service: "gmail",
        auth: {
          user: process.env.GMAIL,
          pass: process.env.GMAIL_PASSWORD,
        },
      });
      let mailOptions = {
        from: "checkmate.sdp@gmail.com",
        to: email,
        subject: "Verification code for Checkmate profile.",
        text: "Your OTP is : " + OTP,
      };

      transporter.sendMail(mailOptions, function (error, info) {
        if (error) {
          console.log(error);
        } else {
          console.log("Email sent: " + info.response);
          res.render("verify");
        }
      });
    }
  );
};

This verifies the user and enters values in database: (I haven't added anything here yet)

exports.verify = (req, res) => {
  console.log(req.body);
};
Ryan143
  • 29
  • 2

1 Answers1

0

Just an overview for you

signup.js

'use-strict';
 exports.signup = (params) => { console.log("Hit", params) }

controller.js

'use-strict';
 var signup = require('./signup');
 var http = require('http');
 
 // you can now access the signup function, 
 signup.signup({username: 'test', password: 'test'})

Looks like you want this to be an HTTP endpoint reciever,

depending on what library youre using, example with Koa route

Backend-- signup.js

 var route = require('koa-route');
 
 exports.init = (app) => {
     app.use(route.post('/signup', signup));
 }


 async function signup(ctx) {
    var body = ctx.request.body;

    //operate
 }

Frontend --

$.ajax({
    url: "/signup",
    type: "post",
    data: JSON.stringify({username: 'get from html', password: 'get from html'})
});
Joe
  • 1,316
  • 9
  • 17
  • Actually, both signup and verify are in one file name auth in my case. I'm trying to take name, email, password and the otp from signup and use it in verify. I'm trying your solution too but could you look at the Image I attached. it might give you a clear picture, https://justpaste.it/4vapo – Ryan143 Oct 11 '21 at 15:33