feat: add api key check in api route

This commit is contained in:
BennyKok
2023-12-15 19:18:42 +08:00
parent 72d0364fee
commit 9aa2484872
4 changed files with 39 additions and 21 deletions
+7
View File
@@ -0,0 +1,7 @@
import { z } from "zod";
export const APIKeyBodyRequest = z.object({
user_id: z.string().optional(),
org_id: z.string().optional(),
iat: z.number(),
});
+14
View File
@@ -0,0 +1,14 @@
import { APIKeyBodyRequest } from "@/server/APIKeyBodyRequest";
import jwt from "jsonwebtoken";
export function parseJWT(token: string) {
try {
// Verify the token - this also decodes it
const decoded = jwt.verify(token, process.env.JWT_SECRET!);
return APIKeyBodyRequest.parse(decoded);
} catch (err) {
// Handle error (token is invalid, expired, etc.)
console.error(err);
return null;
}
}