Fix login and registration issues by removing rate limiting for authenticated users

Replit-Commit-Author: Agent
Replit-Commit-Session-Id: 705f2157-ef97-4fbd-89e4-8c7f2ecaea90
Replit-Commit-Screenshot-Url: https://storage.googleapis.com/screenshot-production-us-central1/7ed01c5f-a82d-405a-b728-b2e3d127c60c/cc224931-556d-4672-8c91-ae793c11d251.jpg
This commit is contained in:
alphaeusmote
2025-04-08 21:22:56 +00:00
parent 061ff37c63
commit a0b0c23f18
3 changed files with 20 additions and 62 deletions
-16
View File
@@ -19,22 +19,6 @@ app.use(compression());
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
// Apply rate limiting middleware for API routes
const apiLimiter = rateLimit({
windowMs: 15 * 60 * 1000, // 15 minutes
max: 100, // limit each IP to 100 requests per windowMs
standardHeaders: true, // Return rate limit info in the `RateLimit-*` headers
legacyHeaders: false, // Disable the `X-RateLimit-*` headers
message: { message: 'Too many requests, please try again later.' },
skip: (req) => {
// Skip rate limiting for authenticated users with admin role
return req.isAuthenticated() && req.user?.role === 'admin';
}
});
// Apply the rate limiter to API routes
app.use('/api/', apiLimiter);
// HTTP request logging (in development mode)
if (process.env.NODE_ENV !== 'production') {
app.use(morgan('dev', {
+20
View File
@@ -5,6 +5,7 @@ import { setupSwagger } from "./swagger";
import { storage } from "./storage";
import { apiQuerySchema, PERMISSIONS } from "@shared/schema";
import { ZodError } from "zod";
import rateLimit from "express-rate-limit";
import {
requireAuth,
requirePermission,
@@ -21,6 +22,25 @@ export async function registerRoutes(app: Express): Promise<Server> {
// Initialize Role Based Access Control system
await initializeRBAC();
// Apply rate limiting middleware for API routes
const apiLimiter = rateLimit({
windowMs: 15 * 60 * 1000, // 15 minutes
max: 100, // limit each IP to 100 requests per windowMs
standardHeaders: true, // Return rate limit info in the `RateLimit-*` headers
legacyHeaders: false, // Disable the `X-RateLimit-*` headers
message: { message: 'Too many requests, please try again later.' },
skip: (req: any) => {
// Skip rate limiting for authenticated users with admin role
if (req.isAuthenticated && typeof req.isAuthenticated === 'function' && req.isAuthenticated()) {
return req.user?.role === 'admin';
}
return false;
}
});
// Apply the rate limiter to API routes
app.use('/api/', apiLimiter);
// Error handler for Zod validation errors
const handleZodError = (err: ZodError, res: Response) => {