Skip to content

Commit fd497a6

Browse files
authored
Merge pull request #613 from devforth/feature/AdminForth/1484/add-ratelimiter-docs
docs: add RateLimiter usage instructions for API
2 parents 867b787 + 59dfe06 commit fd497a6

1 file changed

Lines changed: 43 additions & 0 deletions

File tree

adminforth/documentation/docs/tutorial/03-Customization/12-security.md

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,3 +242,46 @@ export const admin = new AdminForth({
242242
```
243243
244244
Now, if a user’s field `status` is changed to "banned", they won’t be able to perform any actions and moreover will be automatically logged out upon accessing the page.
245+
246+
## RateLimiter for API
247+
248+
### Import
249+
```ts
250+
import { RateLimiter } from "adminforth";
251+
```
252+
253+
### Usage
254+
```ts
255+
import { RateLimiter } from "adminforth";
256+
257+
const UserRateLimiter = new RateLimiter("20/1d");
258+
259+
app.post(
260+
`${ADMIN_BASE_URL}/api/some-api/`,
261+
admin.express.authorize(async (req: any, res: any) => {
262+
263+
const allowed = await UserRateLimiter.consume(req.user.id);
264+
265+
if (!allowed) {
266+
res.status(429).json({
267+
error: "Rate limit exceeded"
268+
});
269+
return;
270+
}
271+
272+
// your API logic here
273+
})
274+
);
275+
```
276+
277+
### Limit format
278+
"20/1d"
279+
This means that a user is allowed to make up to 20 requests within one day, and once this limit is reached, any further requests will be blocked until the 24-hour period resets.
280+
281+
### Supported time units
282+
- s → seconds (10s)
283+
- m → minutes (5m)
284+
- h → hours (1h)
285+
- d → days (1d)
286+
287+
> ☝ Сonsume(key) is used to check whether a specific key such as a userId, IP address, or any other identifier has exceeded its allowed request limit. If the limit has not been reached, it returns true, meaning the request is allowed to proceed.

0 commit comments

Comments
 (0)