Rate limiting token

This commit is contained in:
DrMint 2024-05-12 16:41:12 +02:00
parent 74c5668101
commit 533e182d0d
3 changed files with 9 additions and 4 deletions

View File

@ -23,3 +23,5 @@ SFTP_DESTINATION_PATH_ROOT="/absolute/path/to/destination/root/folder"
SFTP_BASE_URL=https://base-url.com
PAYLOAD_PUBLIC_FRONTEND_BASE_URL=https://domain.com
RATING_LIMIT_SKIP_TOKEN=ratelimitskiptoken5e6ea45ef4e66eaa151612bdcb599df

View File

@ -4,7 +4,6 @@ import { cloudStorage } from "@payloadcms/plugin-cloud-storage";
import path from "path";
import { buildConfig } from "payload/config";
import { sftpAdapter } from "payloadcms-sftp-storage";
import { mustBeAdmin } from "./accesses/collections/mustBeAdmin";
import { Audios } from "./collections/Audios/Audios";
import { ChronologyEvents } from "./collections/ChronologyEvents/ChronologyEvents";
import { Collectibles } from "./collections/Collectibles/Collectibles";
@ -91,7 +90,7 @@ export default buildConfig({
rateLimit: {
window: 900_000, // 15 minutes
max: 500,
skip: (req) => !!mustBeAdmin({ req }),
skip: (request) => request.headers["x-rate-limit-skip"] === process.env.RATING_LIMIT_SKIP_TOKEN,
},
plugins: [
cloudStorage({

View File

@ -74,7 +74,11 @@ const getToken = async (): Promise<string> => {
const injectAuth = async (init?: RequestInit): Promise<RequestInit> => ({
...init,
headers: { ...init?.headers, Authorization: `JWT ${await getToken()}` },
headers: {
...init?.headers,
Authorization: `JWT ${await getToken()}`,
"x-rate-limit-skip": process.env.PAYLOAD_RATING_LIMIT_SKIP_TOKEN ?? "",
},
});
const logResponse = (res: Response) => console.log(res.status, res.statusText, res.url);