fix: update plugin api call

This commit is contained in:
BennyKok
2023-12-15 19:15:41 +08:00
parent bbb7f398ab
commit 72d0364fee
6 changed files with 135 additions and 19 deletions
+22 -11
View File
@@ -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,
})