4.1 React Query 설치 및 Provider 구성 o
This commit is contained in:
16
src/app/QueryProvider.tsx
Normal file
16
src/app/QueryProvider.tsx
Normal file
@@ -0,0 +1,16 @@
|
||||
"use client";
|
||||
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
|
||||
import { ReactQueryDevtools } from "@tanstack/react-query-devtools";
|
||||
import { useState } from "react";
|
||||
|
||||
export default function QueryProvider({ children }: { children: React.ReactNode }) {
|
||||
const [client] = useState(() => new QueryClient());
|
||||
return (
|
||||
<QueryClientProvider client={client}>
|
||||
{children}
|
||||
<ReactQueryDevtools initialIsOpen={false} />
|
||||
</QueryClientProvider>
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -2,8 +2,13 @@ import { NextResponse } from "next/server";
|
||||
import prisma from "@/lib/prisma";
|
||||
import { loginSchema } from "@/lib/validation/auth";
|
||||
import { verifyPassword } from "@/lib/password";
|
||||
import { getClientKey, isRateLimited } from "@/lib/ratelimit";
|
||||
|
||||
export async function POST(req: Request) {
|
||||
const key = getClientKey(req, "login");
|
||||
if (isRateLimited(key, 5, 60_000)) {
|
||||
return NextResponse.json({ error: "Too Many Requests" }, { status: 429 });
|
||||
}
|
||||
const body = await req.json();
|
||||
const parsed = loginSchema.safeParse(body);
|
||||
if (!parsed.success)
|
||||
|
||||
@@ -2,8 +2,13 @@ import { NextResponse } from "next/server";
|
||||
import { loginSchema } from "@/lib/validation/auth";
|
||||
import prisma from "@/lib/prisma";
|
||||
import { verifyPassword } from "@/lib/password";
|
||||
import { getClientKey, isRateLimited } from "@/lib/ratelimit";
|
||||
|
||||
export async function POST(req: Request) {
|
||||
const key = getClientKey(req, "login");
|
||||
if (isRateLimited(key, 5, 60_000)) {
|
||||
return NextResponse.json({ error: "Too Many Requests" }, { status: 429 });
|
||||
}
|
||||
const body = await req.json();
|
||||
const parsed = loginSchema.safeParse(body);
|
||||
if (!parsed.success)
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import type { Metadata } from "next";
|
||||
import "./globals.css";
|
||||
import QueryProvider from "@/app/QueryProvider";
|
||||
|
||||
|
||||
export const metadata: Metadata = {
|
||||
@@ -14,8 +15,10 @@ export default function RootLayout({
|
||||
}>) {
|
||||
return (
|
||||
<html lang="en">
|
||||
<body>
|
||||
{children}
|
||||
<body>
|
||||
<QueryProvider>
|
||||
{children}
|
||||
</QueryProvider>
|
||||
</body>
|
||||
</html>
|
||||
);
|
||||
|
||||
23
src/lib/ratelimit.ts
Normal file
23
src/lib/ratelimit.ts
Normal file
@@ -0,0 +1,23 @@
|
||||
type Key = string;
|
||||
|
||||
const bucket: Map<Key, number[]> = new Map();
|
||||
|
||||
export function getClientKey(req: Request, extra?: string): string {
|
||||
const ip = req.headers.get("x-forwarded-for")?.split(",")[0]?.trim() || "local";
|
||||
return extra ? `${ip}:${extra}` : ip;
|
||||
}
|
||||
|
||||
export function isRateLimited(key: string, max: number, windowMs: number): boolean {
|
||||
const now = Date.now();
|
||||
const windowStart = now - windowMs;
|
||||
const arr = bucket.get(key)?.filter((t) => t >= windowStart) ?? [];
|
||||
if (arr.length >= max) {
|
||||
bucket.set(key, arr); // cleanup
|
||||
return true;
|
||||
}
|
||||
arr.push(now);
|
||||
bucket.set(key, arr);
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user