Skip to content

Commit b43fc8a

Browse files
committed
fix(fullstack-auth): add rate limiting to login route
1 parent 11d8b8b commit b43fc8a

2 files changed

Lines changed: 11 additions & 1 deletion

File tree

template-library/fullstack-auth/backend/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,15 @@
1616
"cors": "^2.8.5",
1717
"dotenv": "^16.3.1",
1818
"express": "^4.18.2",
19+
"express-rate-limit": "^7.1.5",
1920
"jsonwebtoken": "^9.0.2",
2021
"mongoose": "^8.0.3"
2122
},
2223
"devDependencies": {
2324
"@types/bcryptjs": "^2.4.6",
2425
"@types/cors": "^2.8.17",
2526
"@types/express": "^4.17.21",
27+
"@types/express-rate-limit": "^6.0.0",
2628
"@types/jsonwebtoken": "^9.0.5",
2729
"@types/node": "^20.11.0",
2830
"nodemon": "^3.0.2",

template-library/fullstack-auth/backend/src/routes/auth.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,18 @@
33
import { Router } from 'express'
44
import bcrypt from 'bcryptjs'
55
import jwt from 'jsonwebtoken'
6+
import rateLimit from 'express-rate-limit'
67
import User from '../models/User'
78

89
const router = Router()
910

11+
const loginLimiter = rateLimit({
12+
windowMs: 1 * 60 * 1000, // 1 minute
13+
max: 10, // limit each IP to 10 requests per windowMs
14+
standardHeaders: true,
15+
legacyHeaders: false,
16+
})
17+
1018
router.post('/register', async (req, res) => {
1119
const { username, password } = req.body
1220

@@ -39,7 +47,7 @@ router.post('/register', async (req, res) => {
3947
}
4048
})
4149

42-
router.post('/login', async (req, res) => {
50+
router.post('/login', loginLimiter, async (req, res) => {
4351
const { username, password } = req.body
4452

4553
try {

0 commit comments

Comments
 (0)