mirror of
https://github.com/andwati/order-service.git
synced 2026-10-18 02:12:53 +00:00
feat: add rate limiting middleware and role-based access control
This commit is contained in:
@@ -0,0 +1,11 @@
|
||||
import rateLimit from "express-rate-limit";
|
||||
|
||||
export const authRateLimiter = rateLimit({
|
||||
windowMs: 15 * 60 * 1000, // 15 minutes
|
||||
max: 20,
|
||||
standardHeaders: true,
|
||||
legacyHeaders: false,
|
||||
message: {
|
||||
message: "Too many requests, please try again later",
|
||||
},
|
||||
});
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
import type { Request, Response, NextFunction } from "express";
|
||||
|
||||
export function requireRole(role: "admin" | "customer") {
|
||||
return (req: Request, _res: Response, next: NextFunction) => {
|
||||
if (!req.user) {
|
||||
return next({
|
||||
statusCode: 401,
|
||||
message: "Unauthenticated",
|
||||
});
|
||||
}
|
||||
|
||||
if (req.user.role !== role) {
|
||||
return next({
|
||||
statusCode: 403,
|
||||
message: "Forbidden",
|
||||
});
|
||||
}
|
||||
|
||||
next();
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user