I have a form that needs multipart/form-data because there is a file field in that form.
I am using ExpressJS, and I use multer to handle multipart/formdata.
Now I also need to verify the fields and the JWT in the header.
In my app I use middlewares for application/JSON type and do this like this:
app.delete("/ticket/:ticketId", [
authValidation.validJWTNeeded,
authValidation.verifyIfNotLoggedOut,
authPermission.hasPermissionOrIsSameUser(staffRole),
ticketController.deleteById
]);
In every middleware I pass arguments (req, res, next) and return next when needed, and i works fine.
I didn't manage to do it with multer without sending the file first. In documentation they will do something like this, which is not what i want because the file is already sent :
app.post("/route",
multer.upload.single("avatar"),
function (req, res, next) {
// req.file is the `avatar` file
// req.body will hold the text fields, if there were any
})
)
i tried doing things like this but it didn't work :
app.post("/ticket", [
async (req, res, next) => {
multer.upload.none();
let jwt = //req.headers....... this is the jwt sent
//req.body should have all the fields
return next
},
authValidation.validJWTNeeded, //Here i verify the JWT for auth
authValidation.verifyIfNotLoggedOut, //About the same here
ticketController.verifyFields, // /!\ I need to verify if fields are correct
//function/middleware to upload using multer.upload.single("field"),
ticketController.insert // If everything above passed, then create the ticket and upload the file
]);
So my question is : how do I do all the validation using multer and data sent with multipart/formdata encryption?
Thanks!