Use auth token to identify client request in rate limiter

This commit is contained in:
Hongbo Wu 2022-10-25 12:42:32 +08:00
parent 12472ce094
commit c9ea622461

View file

@ -94,12 +94,20 @@ export const createApp = (): {
app.use(json({ limit: '100mb' }))
app.use(urlencoded({ limit: '100mb', extended: true }))
if (!env.dev.isLocal) {
if (env.dev.isLocal) {
const apiLimiter = rateLimit({
windowMs: 60 * 1000, // 1 minute
max: 50, // Limit each IP to 10 requests per `window` (here, per minute)
standardHeaders: true, // Return rate limit info in the `RateLimit-*` headers
legacyHeaders: false, // Disable the `X-RateLimit-*` headers
keyGenerator: (req) => {
return (
req.header('authorization') ||
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
(req.cookies['auth'] as string) ||
req.ip
)
},
})
// Apply the rate limiting middleware to API calls only
app.use('/api/', apiLimiter)