mirror of
https://github.com/lukevella/rallly.git
synced 2025-08-06 09:59:00 +02:00
✨ Add rate limit middleware for safe actions (#1836)
This commit is contained in:
parent
502048488f
commit
f8bdfdd067
4 changed files with 40 additions and 14 deletions
|
@ -1,24 +1,19 @@
|
||||||
"use server";
|
"use server";
|
||||||
|
|
||||||
import { feedbackSchema } from "@/features/feedback/schema";
|
import { feedbackSchema } from "@/features/feedback/schema";
|
||||||
import { authActionClient } from "@/features/safe-action/server";
|
import {
|
||||||
|
authActionClient,
|
||||||
|
createRateLimitMiddleware,
|
||||||
|
} from "@/features/safe-action/server";
|
||||||
import { getEmailClient } from "@/utils/emails";
|
import { getEmailClient } from "@/utils/emails";
|
||||||
import { rateLimit } from "../rate-limit";
|
|
||||||
|
|
||||||
export const submitFeedbackAction = authActionClient
|
export const submitFeedbackAction = authActionClient
|
||||||
.metadata({
|
.metadata({
|
||||||
actionName: "submitFeedback",
|
actionName: "submit_feedback",
|
||||||
})
|
})
|
||||||
|
.use(createRateLimitMiddleware(5, "1 h"))
|
||||||
.inputSchema(feedbackSchema)
|
.inputSchema(feedbackSchema)
|
||||||
.action(async ({ ctx, parsedInput }) => {
|
.action(async ({ ctx, parsedInput }) => {
|
||||||
const { success } = await rateLimit("submitFeedback", 3, "1h");
|
|
||||||
|
|
||||||
if (!success) {
|
|
||||||
return {
|
|
||||||
error: "Rate limit exceeded" as const,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const { content } = parsedInput;
|
const { content } = parsedInput;
|
||||||
getEmailClient().sendEmail({
|
getEmailClient().sendEmail({
|
||||||
|
|
|
@ -8,8 +8,8 @@ import { auth } from "@/next-auth";
|
||||||
|
|
||||||
import { isRateLimitEnabled } from "./constants";
|
import { isRateLimitEnabled } from "./constants";
|
||||||
|
|
||||||
type Unit = "ms" | "s" | "m" | "h" | "d";
|
export type Unit = "ms" | "s" | "m" | "h" | "d";
|
||||||
type Duration = `${number} ${Unit}` | `${number}${Unit}`;
|
export type Duration = `${number} ${Unit}` | `${number}${Unit}`;
|
||||||
|
|
||||||
async function getIPAddress() {
|
async function getIPAddress() {
|
||||||
return (await headers()).get("x-forwarded-for");
|
return (await headers()).get("x-forwarded-for");
|
||||||
|
|
|
@ -32,6 +32,11 @@ export const useSafeAction: typeof useAction = (action, options) => {
|
||||||
defaultValue: "An internal server error occurred",
|
defaultValue: "An internal server error occurred",
|
||||||
});
|
});
|
||||||
break;
|
break;
|
||||||
|
case "TOO_MANY_REQUESTS":
|
||||||
|
translatedDescription = t("actionErrorTooManyRequests", {
|
||||||
|
defaultValue: "You are making too many requests",
|
||||||
|
});
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
toast.error(translatedDescription);
|
toast.error(translatedDescription);
|
||||||
|
|
|
@ -6,12 +6,15 @@ import { revalidatePath } from "next/cache";
|
||||||
import { createMiddleware, createSafeActionClient } from "next-safe-action";
|
import { createMiddleware, createSafeActionClient } from "next-safe-action";
|
||||||
import z from "zod";
|
import z from "zod";
|
||||||
import { requireUserAbility } from "@/auth/queries";
|
import { requireUserAbility } from "@/auth/queries";
|
||||||
|
import type { Duration } from "@/features/rate-limit";
|
||||||
|
import { rateLimit } from "@/features/rate-limit";
|
||||||
|
|
||||||
type ActionErrorCode =
|
type ActionErrorCode =
|
||||||
| "UNAUTHORIZED"
|
| "UNAUTHORIZED"
|
||||||
| "NOT_FOUND"
|
| "NOT_FOUND"
|
||||||
| "FORBIDDEN"
|
| "FORBIDDEN"
|
||||||
| "INTERNAL_SERVER_ERROR";
|
| "INTERNAL_SERVER_ERROR"
|
||||||
|
| "TOO_MANY_REQUESTS";
|
||||||
|
|
||||||
export class ActionError extends Error {
|
export class ActionError extends Error {
|
||||||
code: ActionErrorCode;
|
code: ActionErrorCode;
|
||||||
|
@ -65,6 +68,29 @@ const posthogMiddleware = createMiddleware<{
|
||||||
return result;
|
return result;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
export const createRateLimitMiddleware = (
|
||||||
|
requests: number,
|
||||||
|
duration: Duration,
|
||||||
|
) =>
|
||||||
|
createMiddleware<{
|
||||||
|
metadata: {
|
||||||
|
actionName: string;
|
||||||
|
};
|
||||||
|
}>().define<{
|
||||||
|
ctx: { user: { id: string } };
|
||||||
|
}>(async ({ next, metadata }) => {
|
||||||
|
const res = await rateLimit(metadata.actionName, requests, duration);
|
||||||
|
|
||||||
|
if (!res.success) {
|
||||||
|
throw new ActionError({
|
||||||
|
code: "TOO_MANY_REQUESTS",
|
||||||
|
message: "You are making too many requests.",
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
return next();
|
||||||
|
});
|
||||||
|
|
||||||
export const actionClient = createSafeActionClient({
|
export const actionClient = createSafeActionClient({
|
||||||
defineMetadataSchema: () =>
|
defineMetadataSchema: () =>
|
||||||
z.object({
|
z.object({
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue