GUMROAD license verification API with NodeJs

Viewed 182

I have the below middleware function which controls my user register page.

in the specific the below function is able to:

  • recognizes if the user insert an email that is already in database;
  • recognizes if the password and password confirm coincide;
  • and, consuming the Gumroad API is recognizing if the licensekey insert is valid or not;

I just added the code for the gumroad API for license key verification, is working, but on terminal I got the following error:

(node:23536) UnhandledPromiseRejectionWarning: TypeError: req.next is not a function at done (C:\Users\CESARE\User-Management-Tutorial\node_modules\express\lib\response.js:1007:25) at ExpressHandlebars.renderView (C:\Users\CESARE\User-Management-Tutorial\node_modules\express-handlebars\lib\express-handlebars.js:244:4) (node:23536) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag --unhandled-rejections=strict (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 18)

Here below the authController.js file:

exports.register = (req, res) => {
    // Destructor
    const { name, email, password, passwordConfirm, licencekey } = req.body;

    var url = "https://api.gumroad.com/v2/licenses/verify";

    var xhr = new XMLHttpRequest();
    xhr.open("POST", url);

    xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");

    xhr.onreadystatechange = function () {
        if (xhr.readyState === 4) {
        
            console.log(xhr.responseText);

        }

      
        


        connection.query('SELECT email FROM login WHERE email = ?', [email], async (error, results) => {
          
            if (error) {
                console.log(error);
            }
            if (results.length > 0) {
                return res.render('register', {
                    message: 'That email is already in use'
                })
            } else if (password !== passwordConfirm) {
                return res.render('register', {
                    message: 'Password do not match'
                });
            }  

            let hashedPassword = await bcrypt.hash(password, 8);


            connection.query('INSERT INTO login SET ?', { name: name, email: email, password: hashedPassword }, (error, results) => {
                if (error) {
                    console.log(error);
                } else {
                    return res.render('register', {
                        message: 'User registered'
                    });

                }
            })

        });
      
        if (xhr.status != 200) {
            return res.render('register', {
                message: 'License number not valid'
            })
        }
    }
   
    var data = "product_permalink=dpduj" + "&" + "license_key=" + `${licencekey}`
    xhr.send(data);

};

Any advise/direction on how to remove this error are highly appreciated, thank you!

0 Answers
Related