feat: add new auth_request flow for logging in with comfy deploy

This commit is contained in:
BennyKok
2024-01-22 14:11:14 +08:00
parent 3043093d22
commit 47168930dc
25 changed files with 2424 additions and 272 deletions
+3 -2
View File
@@ -1,9 +1,10 @@
import { z } from "zod";
export const APIKeyBodyRequest = z.object({
user_id: z.string().optional(),
org_id: z.string().optional(),
user_id: z.string().optional().nullable(),
org_id: z.string().optional().nullable(),
iat: z.number(),
exp: z.number().optional(),
});
export type APIKeyUserType = z.infer<typeof APIKeyBodyRequest>;
+41 -1
View File
@@ -1,6 +1,46 @@
import { db } from "@/db/db";
import type { WorkflowVersionType } from "@/db/schema";
import { workflowTable, workflowVersionTable } from "@/db/schema";
import { eq, sql } from "drizzle-orm";
export async function createNewWorkflowVersion({
workflow_id,
workflowData,
}: {
workflow_id: string;
workflowData: Pick<
WorkflowVersionType,
"workflow" | "workflow_api" | "snapshot"
>;
}) {
// Add a new version
const data = await db
.insert(workflowVersionTable)
.values({
workflow_id,
...workflowData,
version: sql`(
SELECT COALESCE(MAX(version), 0) + 1
FROM ${workflowVersionTable}
WHERE workflow_id = ${workflow_id}
)`,
})
.returning();
const version = data[0].version;
// Touch up the last updated time
await db
.update(workflowTable)
.set({
updated_at: new Date(),
})
.where(eq(workflowTable.id, workflow_id))
.returning();
return {
version,
};
}
export async function createNewWorkflow({
workflow_name,
@@ -10,7 +50,7 @@ export async function createNewWorkflow({
}: {
workflow_name: string;
user_id: string;
org_id?: string;
org_id?: string | null;
workflowData: Pick<
WorkflowVersionType,
"workflow" | "workflow_api" | "snapshot"
+17 -12
View File
@@ -1,23 +1,28 @@
"use server";
import { db } from "@/db/db";
import { apiKeyTable } from "@/db/schema";
import { apiKeyTable, authRequestsTable } from "@/db/schema";
import { withServerPromise } from "@/server/withServerPromise";
import { auth } from "@clerk/nextjs";
import { and, desc, eq, isNull } from "drizzle-orm";
import jwt from "jsonwebtoken";
import { revalidatePath } from "next/cache";
// export const nanoid = customAlphabet(
// "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"
// );
export const createAuthRequest = withServerPromise(
async (request_id: string) => {
const { userId, orgId } = auth();
// const prefixes = {
// cd: "cd",
// } as const;
const result = await db.insert(authRequestsTable).values({
request_id: request_id,
user_id: userId,
org_id: orgId,
});
// function newId(prefix: keyof typeof prefixes): string {
// return [prefixes[prefix], nanoid(16)].join("_");
// }
return {
message: "Auth request created, you may now return to your application.",
};
},
);
export async function addNewAPIKey(name: string) {
const { userId, orgId } = auth();
@@ -29,7 +34,7 @@ export async function addNewAPIKey(name: string) {
if (orgId) {
token = jwt.sign(
{ user_id: userId, org_id: orgId },
process.env.JWT_SECRET!
process.env.JWT_SECRET!,
);
} else {
token = jwt.sign({ user_id: userId }, process.env.JWT_SECRET!);
@@ -93,7 +98,7 @@ export async function getAPIKeys() {
where: and(
eq(apiKeyTable.user_id, userId),
isNull(apiKeyTable.org_id),
eq(apiKeyTable.revoked, false)
eq(apiKeyTable.revoked, false),
),
orderBy: desc(apiKeyTable.created_at),
});
@@ -0,0 +1,18 @@
import { db } from "@/db/db";
import { usersTable } from "@/db/schema";
import { clerkClient } from "@clerk/nextjs";
import { eq } from "drizzle-orm";
export async function getOrgOrUserDisplayName(
orgId: string | undefined | null,
userId: string,
) {
return orgId
? await clerkClient.organizations
.getOrganization({
organizationId: orgId,
})
.then((x) => x.name)
: (await db.select().from(usersTable).where(eq(usersTable.id, userId)))[0]
.name;
}