Skip to content

Commit 1cf905a

Browse files
authored
Merge pull request #618 from devforth/next
Next
2 parents 2f78baa + ca2f43a commit 1cf905a

6 files changed

Lines changed: 48 additions & 4 deletions

File tree

adminforth/dataConnectors/clickhouse.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -530,7 +530,6 @@ class ClickhouseConnector extends AdminForthBaseConnector implements IAdminForth
530530
}).join(', ');
531531
const tableName = resource.table;
532532

533-
console.log('getDataWithOriginalTypes called with filters', JSON.stringify(filters), 'and sort', JSON.stringify(sort));
534533
const { where, params } = this.whereClause(resource, filters);
535534

536535
const orderBy = sort.length ? `ORDER BY ${sort.map((s) => `${s.field} ${this.SortDirectionsMap[s.direction]}`).join(', ')}` : '';

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.

adminforth/modules/restApi.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -718,6 +718,7 @@ export default class AdminForthRestAPI implements IAdminForthRestAPI {
718718

719719

720720
if (!userRecord) {
721+
response.setStatus(401);
721722
return { error: INVALID_MESSAGE };
722723
}
723724

@@ -748,6 +749,7 @@ export default class AdminForthRestAPI implements IAdminForthRestAPI {
748749
});
749750
}
750751
} else {
752+
response.setStatus(401);
751753
return { error: INVALID_MESSAGE };
752754
}
753755

adminforth/spa/src/utils/listUtils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ export async function startBulkAction(actionId: string, resource: AdminForthReso
6161
if (action?.confirm) {
6262
const confirmed = await confirm({
6363
title: action.confirm,
64-
message: `${t('Deleting')} ${checkboxes.value.length} ${checkboxes.value.length === 1 ? t('item') : t('items')}. ${t('This process is irreversible.')}`,
64+
message: t('Deleting {count} item. This process is irreversible. | Deleting {count} items. This process is irreversible.', { count: checkboxes.value.length }),
6565
});
6666
if (!confirmed) {
6767
return;

adminforth/spa/src/utils/utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ export async function callApi({path, method, body, headers, silentError = false,
141141
const fullPath = `${import.meta.env.VITE_ADMINFORTH_PUBLIC_PATH || ''}${path}`;
142142
try {
143143
const r = await fetch(fullPath, options);
144-
if (r.status == 401 ) {
144+
if (r.status == 401 && !path.includes('/login')) {
145145
useUserStore().unauthorize();
146146
useCoreStore().resetAdminUser();
147147
await redirectToLogin();

adminforth/types/Back.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ export interface IConfigValidator {
3636

3737
export interface IAdminForthHttpResponse {
3838
setHeader: (key: string, value: string) => void,
39-
setStatus: (code: number, message: string) => void,
39+
setStatus: (code: number, message?: string) => void,
4040
blobStream: () => Writable,
4141
};
4242

0 commit comments

Comments
 (0)