Repository navigation
Expand file tree
/
Copy pathproxy.ts
More file actions
56 lines (49 loc) · 1.92 KB
/
Copy pathproxy.ts
File metadata and controls
56 lines (49 loc) · 1.92 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
import { headers } from "next/headers";
import { NextRequest, NextResponse } from "next/server";
import { stackServerApp } from "./stack/server";
import { ReadonlyHeaders } from "next/dist/server/web/spec-extension/adapters/headers";
import { ServerUser } from "@stackframe/stack";
import { ApiKey, ApiKeys } from "@/app/_types";
export const config = {
matcher: "/api/:path*",
};
export async function proxy(request: NextRequest) {
const headersList: ReadonlyHeaders = await headers();
const userId: string | null = headersList.get("Netflix-User-ID");
const apiKey: string | null = headersList.get("Netflix-API-Key");
try {
if (!userId || !apiKey)
return NextResponse.json(
{ message: "ไม่มีการยืนยันตัวตนของผู้ใช้งาน!" },
{ status: 401 }
);
if (apiKey.length !== 102)
return NextResponse.json(
{ message: "API Key ไม่ถูกต้อง!" },
{ status: 403 }
);
const user: ServerUser | null = await stackServerApp.getUser(userId);
if (!user)
return NextResponse.json(
{ message: "id ยืนยันตัวตนของผู้ใช้งานไม่ถูกต้อง!" },
{ status: 403 }
);
const apiKeys: ApiKeys = await user.listApiKeys();
const matchingKeys: ApiKeys = apiKeys.filter(
(k: ApiKey): boolean =>
k.userId === userId &&
apiKey.length === 102 &&
apiKey.endsWith(k.value.lastFour)
);
const key: ApiKey | undefined = matchingKeys.pop();
if (key && key.isValid()) return NextResponse.next();
return NextResponse.json(
{ message: "ไม่รับอนุญาตให้เข้าถึงข้อมูล!" },
{ status: 403 }
);
} catch (err: unknown) {
if (err instanceof Error) {
return NextResponse.json({ error: err.message }, { status: 500 });
}
}
}