Multer AWS S3 problem

Hello all,
I am working on an express application which involves profile image upload during registration. But it seems the form submits before the image gets uploaded to aws s3 and this makes it impossible to save the file url to my mongo database.
Any ideas how to tackle this?

//My S3 Config
var bucketPath = 'MYBUCKET';
aws.config.update({
  accessKeyId: "MYKEY",
  secretAccessKey: "MYSECRETACCESSKEY",
  "region": "region"
});
var s3 = new aws.S3(bucketPath);
 
var upload = multer({
  storage: multerS3({
    s3: s3,
    bucket: 'MYBUCKET',
    acl: 'public-read',
    metadata: function (req, file, cb) {
      cb(null, {fieldName: file.fieldname});
    },
    key: function (req, file, cb) {
      cb(null, Date.now().toString())
    }
  })
})
//Post request
router.post('/add/student', upload.single('passport'), function(req, res, next) {
  
  var userId    = req.body.userId.toLowerCase();
  var password  = req.body.password.toLowerCase();
  var password2 = req.body.password2.toLowerCase();
  var name = req.body.name;
  if(req.file){
    var passport = req.file.location;
}else{
	var passport = 'noimage.png';
}

var newStudent = new Student({
   userId: userId,
   password: password,
   name: name,
   passport : passport 
});

User.createStudent(newStudent, function(error, student){
     if (error){
         throw error;
      }
     req.flash('success', 'Student registeration successful');
     res.location('/admin');
     res.redirect('/admin');
});

Can you share your code?

I just edited my earlier post. This is driving me crazy

Some security issues / to-dos in your code: Converting passwords to lowercase reduces the security and entropy of them. In addition what happens when numbers or symbols are included with the toLowerCase method? Make sure to hash your passwords with bcrypt to keep them safely stored before this goes into production.

Okay boss, thanks for pointing that out

1 Like