-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmiddleware.ts
More file actions
64 lines (51 loc) · 2.29 KB
/
middleware.ts
File metadata and controls
64 lines (51 loc) · 2.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import { auth } from "@/auth";
import { NextResponse } from "next/server";
import type { NextRequest } from "next/server";
const rateLimit = new Map<string, { count: number; resetTime: number }>();
function checkRateLimit(ip: string, limit: number = 60): boolean {
const now = Date.now();
const windowMs = 60 * 1000;
const record = rateLimit.get(ip);
if (!record || now > record.resetTime) {
rateLimit.set(ip, { count: 1, resetTime: now + windowMs });
return true;
}
if (record.count >= limit) return false;
record.count++;
return true;
}
export default auth((req: any) => {
const ip = req.headers.get("x-forwarded-for") ?? "unknown";
const path = req.nextUrl.pathname;
// ─── Studio Protection ─────────────────────────────────────────────────────
if (path.startsWith("/studio")) {
const adminEmail = process.env.ADMIN_EMAIL;
if (req.auth?.user?.email !== adminEmail) {
return NextResponse.redirect(new URL("/", req.url));
}
}
// ─── Rate Limiting ─────────────────────────────────────────────────────────
if (path.startsWith("/api/") && !path.startsWith("/api/auth")) {
if (!checkRateLimit(ip, 30)) {
return NextResponse.json(
{ error: "Too many requests" },
{ status: 429 }
);
}
}
// ─── Onboarding Guard ──────────────────────────────────────────────────────
const isLoggedIn = !!req.auth;
const isOnboarded = req.auth?.isOnboarded;
if (isLoggedIn && !isOnboarded && path !== "/onboarding" && !path.startsWith("/api")) {
return NextResponse.redirect(new URL("/onboarding", req.url));
}
if (isLoggedIn && isOnboarded && path === "/onboarding") {
return NextResponse.redirect(new URL("/", req.url));
}
return NextResponse.next();
});
export const config = {
matcher: [
"/((?!_next/static|_next/image|favicon.ico|studio).*)",
],
};