Authenticate before receiving multipart form data

hgs-hcoard created this issue on 2021-10-12 · The issue is replied 2 times

I have a route that accepts a file via multipart form data and saves it to the file system using multer. I am using passport to authenticate the user's jwt token. The route looks something like this:

router.post(['/'],
passport.authenticate('keycloak', { session: false }),
multer.single('file')
);

The problem is that the files I am uploading are very large. It can take 10+ minutes for the app to receive the data. By the time passport authenticates the token, it is always expired since it compares it to the current time, not the time the request was started. If the token is invalid for any reason, the user won't know until 10 minutes later. I would like the user to get immediate feedback instead of waiting for the request body to be received. I tried using passport as an app level middleware, but it still waited until it had req.body before executing.

Is there a way to execute middleware before receiving the request body?

dougwilson wrote this answer on 2021-10-12

So unless there is more to your app than you shared, then that is what Express is doing. Express does not populate req.body or read the body without you adding middlewares to do it. If you have an express server where what you posted above was the only route, it would have the behavior you are looking for.

Can you share a full reproduction of the issue? It is difficult to help otherwise.

dougwilson wrote this answer on 2021-11-17

Closing due to no response.

🤔
Didn't find what you were looking for ?
Create your own issue
More Details About Repo
Owner Name expressjs
Repo Name express
Full Name expressjs/express
Language JavaScript
Created Date 2009-06-26
Updated Date 2022-01-18
Star Count 55638
Watcher Count 1763
Fork Count 9413
Issue Count 157

YOU MAY BE INTERESTED

Issue Title State Comments Created Date Updated Date Closed Date