I am able to upload files to my database (Mongodb) However, is it possible to know who uploaded the file using passport? i tried something like this but it doesnt work.

app.post('/upload', upload.single('file'), (req, res) => {
  // res.json({ file: req.file });
  db.collection("anyCollection").insertOne({
    fileUploaded : file,
    uploadedBy   : req.user
});

});

Gridfs Init :

let conn = mongoose.createConnection("mongodb://localhost:27017/FYP")
// Init gfs
let gfs;

conn.once('open', () => {
  // Init stream
  gfs = Grid(conn.db, mongoose.mongo);
  gfs.collection('requests');
});

My storage engine :

// Create storage engine
const storage = new GridFsStorage({
    url: "mongodb://localhost:27017/FYP",
    file: (req, file) => {
      return new Promise((resolve, reject) => {
        crypto.randomBytes(16, (err, buf) => {
          if (err) {
            return reject(err);
          }
          const filename = buf.toString('hex') + path.extname(file.originalname);
          const fileInfo = {
            filename: filename,
            bucketName: 'anyCollection'
          };
          resolve(fileInfo);
        });
      });
    }
  });

  const upload = multer({ storage });

Is there a way to know who is the user who uploaded?

0 Answers