Skip to content

Commit 717b2f1

Browse files
Merge pull request #221 from Harkiratcodess/fix/otp-rate-limiting
fix: add rate limiting to OTP verify-email and resend-otp endpoints
2 parents 70140ab + dcf483e commit 717b2f1

1 file changed

Lines changed: 14 additions & 2 deletions

File tree

server/src/module/auth/auth.routes.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,18 @@ import { Router } from "express";
22
import { AuthController } from "./auth.controller.js";
33
import { AuthService } from "./auth.service.js";
44
import { authMiddleware } from "../../middleware/auth.middleware.js";
5+
import rateLimit from "express-rate-limit";
6+
7+
const createOtpRateLimit = (max: number, message: string) => rateLimit({
8+
windowMs: 15 * 60 * 1000,
9+
max,
10+
message: { message },
11+
standardHeaders: true,
12+
legacyHeaders: false,
13+
});
14+
15+
const verifyEmailRateLimit = createOtpRateLimit(5, "Too many attempts, please try again after 15 minutes");
16+
const resendOtpRateLimit = createOtpRateLimit(3, "Too many resend attempts, please try again after 15 minutes");
517

618
const authService = new AuthService();
719
const authController = new AuthController(authService);
@@ -11,8 +23,8 @@ export const authRouter = Router();
1123
authRouter.post("/register", (req, res) => authController.register(req, res));
1224
authRouter.post("/login", (req, res) => authController.login(req, res));
1325
authRouter.post("/google", (req, res) => authController.googleAuth(req, res));
14-
authRouter.post("/verify-email", (req, res) => authController.verifyEmail(req, res));
15-
authRouter.post("/resend-otp", (req, res) => authController.resendOtp(req, res));
26+
authRouter.post("/verify-email", verifyEmailRateLimit, (req, res) => authController.verifyEmail(req, res));
27+
authRouter.post("/resend-otp", resendOtpRateLimit, (req, res) => authController.resendOtp(req, res));
1628
authRouter.post("/forgot-password", (req, res) => authController.forgotPassword(req, res));
1729
authRouter.post("/reset-password", (req, res) => authController.resetPassword(req, res));
1830
authRouter.post("/logout", (req, res) => authController.logout(req, res));

0 commit comments

Comments
 (0)