0

I'm using FormData() in my React app. I was going to use it for registration and login too. I have a working passport.js registration piece of code and was going to use it with busboy but it looks like it reads fields one by one whenever there is one and it seems like I can't use it with Account.register.

I've inserted Account.register in busboy.on('field') but then realized it won't work. I didn't change req.body.username etc in my original code, ignore them.

busboy.on('field', function(fieldname, val, fieldnameTruncated, valTruncated, encoding, mimetype) {
    console.log('Field [' + fieldname + ']: value: ' + inspect(val));
    Account.register(new Account({ nickname: req.body.username, email: req.body.email }), req.body.passwordOne, (err, user) => {
      if(err){
        helpers.errors(err, res);
      } else {
        helpers.registerSuccess(res);
      }
    });
  });

  busboy.on('finish', function() {
    console.log('Done parsing form!');
    //res.writeHead(303, { Connection: 'close', Location: '/' });
    res.end();
  });
dan tenovski
  • 311
  • 2
  • 10

1 Answers1

0

I'm using nickname instead of username in passport.js because I'm using email as the username field. Working code:

router.post('/register', (req, res, next)=>{
  var busboy = new Busboy({ headers: req.headers });

  let nickname = '';
  let email = '';
  let password = '';

  busboy.on('file', function(fieldname, file, filename, encoding, mimetype) {
    console.log('File [' + fieldname + ']: filename: ' + filename + ', encoding: ' + encoding + ', mimetype: ' + mimetype);
    file.on('data', function(data) {
      console.log('File [' + fieldname + '] got ' + data.length + ' bytes');
    });
    file.on('end', function() {
      console.log('File [' + fieldname + '] Finished');
    });
  });

  busboy.on('field', function(fieldname, val, fieldnameTruncated, valTruncated, encoding, mimetype) {
    console.log('Field [' + fieldname + ']: value: ' + inspect(val));
    if(fieldname == 'username') { nickname = val; }
    if(fieldname == 'email') { email = val; }
    if(fieldname == 'password') { password = val; }

  });

  busboy.on('finish', function() {
    console.log('Done parsing form!');
    console.log('email: ' + email + 'password: ' + password + 'username: ' + nickname);
    Account.register(new Account({ nickname: nickname, email: email }), password, (err, user) => {
      if(err){
        helpers.errors(err, res);
      } else {
        helpers.registerSuccess(res);
      }
    });
  });

  req.pipe(busboy);
});
dan tenovski
  • 311
  • 2
  • 10