fix: update plugin api call
This commit is contained in:
@@ -7,6 +7,7 @@ import {
|
||||
} from "@/db/schema";
|
||||
import { parseDataSafe } from "@/lib/parseDataSafe";
|
||||
import { sql } from "drizzle-orm";
|
||||
import jwt from "jsonwebtoken";
|
||||
import { NextResponse } from "next/server";
|
||||
import { z } from "zod";
|
||||
|
||||
@@ -17,7 +18,7 @@ const corsHeaders = {
|
||||
};
|
||||
|
||||
const UploadRequest = z.object({
|
||||
user_id: z.string(),
|
||||
// user_id: z.string(),
|
||||
workflow_id: z.string().optional(),
|
||||
workflow_name: z.string().optional(),
|
||||
workflow: workflowType,
|
||||
@@ -35,8 +36,39 @@ export async function OPTIONS(request: Request) {
|
||||
});
|
||||
}
|
||||
|
||||
const APIKeyBodyRequest = z.object({
|
||||
user_id: z.string().optional(),
|
||||
org_id: z.string().optional(),
|
||||
iat: z.number(),
|
||||
});
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
export async function POST(request: Request) {
|
||||
console.log("hi");
|
||||
const token = request.headers.get("Authorization")?.split(" ")?.[1]; // Assuming token is sent as "Bearer your_token"
|
||||
const userData = token ? parseJWT(token) : undefined;
|
||||
if (!userData) {
|
||||
return new NextResponse("Invalid or expired token", {
|
||||
status: 401,
|
||||
headers: corsHeaders,
|
||||
});
|
||||
}
|
||||
|
||||
console.log(userData);
|
||||
|
||||
const { user_id, org_id } = userData;
|
||||
|
||||
if (!user_id) return new NextResponse("Invalid user_id", { status: 401 });
|
||||
|
||||
const [data, error] = await parseDataSafe(
|
||||
UploadRequest,
|
||||
@@ -47,7 +79,7 @@ export async function POST(request: Request) {
|
||||
if (!data || error) return error;
|
||||
|
||||
const {
|
||||
user_id,
|
||||
// user_id,
|
||||
workflow,
|
||||
workflow_api,
|
||||
workflow_id: _workflow_id,
|
||||
|
||||
@@ -4,31 +4,42 @@ import { db } from "@/db/db";
|
||||
import { apiKeyTable } from "@/db/schema";
|
||||
import { auth } from "@clerk/nextjs";
|
||||
import { and, desc, eq } from "drizzle-orm";
|
||||
import { customAlphabet } from "nanoid";
|
||||
import jwt from "jsonwebtoken";
|
||||
import { revalidatePath } from "next/cache";
|
||||
|
||||
export const nanoid = customAlphabet(
|
||||
"123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"
|
||||
);
|
||||
// export const nanoid = customAlphabet(
|
||||
// "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"
|
||||
// );
|
||||
|
||||
const prefixes = {
|
||||
cd: "cd",
|
||||
} as const;
|
||||
// const prefixes = {
|
||||
// cd: "cd",
|
||||
// } as const;
|
||||
|
||||
function newId(prefix: keyof typeof prefixes): string {
|
||||
return [prefixes[prefix], nanoid(16)].join("_");
|
||||
}
|
||||
// function newId(prefix: keyof typeof prefixes): string {
|
||||
// return [prefixes[prefix], nanoid(16)].join("_");
|
||||
// }
|
||||
|
||||
export async function addNewAPIKey(name: string) {
|
||||
const { userId, orgId } = auth();
|
||||
|
||||
if (!userId) throw new Error("No user id");
|
||||
|
||||
let token: string;
|
||||
|
||||
if (orgId) {
|
||||
token = jwt.sign(
|
||||
{ user_id: userId, org_id: orgId },
|
||||
process.env.JWT_SECRET!
|
||||
);
|
||||
} else {
|
||||
token = jwt.sign({ user_id: userId }, process.env.JWT_SECRET!);
|
||||
}
|
||||
|
||||
const key = await db
|
||||
.insert(apiKeyTable)
|
||||
.values({
|
||||
name: name,
|
||||
key: newId("cd"),
|
||||
key: token,
|
||||
user_id: userId,
|
||||
org_id: orgId,
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user