feat: add api key check in api route
This commit is contained in:
@@ -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(),
|
||||
});
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user