Hello. I'm trying to skip the API rate limits for requests made by an authenticated user with a particular role. The docs mention a "skip" Express middleware function that can be used, but I'm not sure how to get the user from the request object. Is there an example somewhere using the "skip" property?
I couldn't figure this out. I ended up with a somewhat dumb solution passing a secret as a header
rateLimit: {
trustProxy: true,
skip: ({ headers }) => {
return typeof headers['x-api-key'] !== undefined && headers['x-api-key'] === process.env.PAYLOAD_ADMIN_API_KEY;
}
},
I believe that the user should be available on the req object in express (as long as payload.authenticate() is called)
skip: (request, response) => {
console.log(request.user)
return false // no requests skipped by default
}
Actually, not sure you need
payload.authenticate()
before this, but IIRC that tells express to check the session
I'm on Payload version 2.30.3 and
request.user
is always undefined in the skip function
The authentication middleware gets called way after the rateLimit one, so user wouldn't be available yet.
Star
Discord
online
Get dedicated engineering support directly from the Payload team.