-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmiddleware.ts
44 lines (38 loc) · 1.23 KB
/
middleware.ts
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
export const dynamic = 'force-dynamic';
import { NextRequest } from 'next/server';
import { NextResponse } from 'next/server';
import { MemoryStore } from '@/utility/MemoryStore';
// import Pocketbase from 'pocketbase';
// import { cookies } from 'next/headers';
const store = new MemoryStore(5000);
export function middleware(request: NextRequest) {
const pathName = request.nextUrl.pathname;
/**
* RATE LIMITS THE API REQS - USES IP ADDRESS
*/
// TODO: UNCOMMENT THIS IN PRODUCTION
// if (pathName.startsWith('/api/')) {
// if (!request.ip) {
// return NextResponse.json(
// { ok: false, error: 'ACCESS_DENIED: NO IP ADDRESS' },
// { status: 403 }
// );
// }
// const maxReq = 5;
// const key = request.ip;
// const hits = store.increment(key);
// if (hits > maxReq) {
// return NextResponse.json(
// { ok: false, error: 'TOO_MANY_REQUESTS' },
// { status: 429, headers: [['Retry-After', '10']] }
// );
// } else {
// return NextResponse.next();
// }
// }
// JUST AN EXAMPLE FOR LATER USE
// return NextResponse.json(
// { ok: false, error: 'TOO_MANY_REQUESTS' },
// { status: 429, headers: [['random-header', 'value']] }
// );
}