From 0316fdc0a3993d212790161a2129578576e99b58 Mon Sep 17 00:00:00 2001 From: charles-gauthereau Date: Mon, 25 May 2026 13:04:42 +0200 Subject: [PATCH] feat: added the api key list, delete, create in profile modal. --- src/features/profile/profile-account.tsx | 29 ++++++++++++++++++------ src/features/profile/profile.action.ts | 29 +++++++++++++++++++++++- src/lib/auth/auth.ts | 10 ++++++++ 3 files changed, 60 insertions(+), 8 deletions(-) diff --git a/src/features/profile/profile-account.tsx b/src/features/profile/profile-account.tsx index 52a93443..abe49b25 100644 --- a/src/features/profile/profile-account.tsx +++ b/src/features/profile/profile-account.tsx @@ -40,6 +40,7 @@ import { Label } from "@/components/ui/label"; import { useState } from "react"; import { timeAgo } from "@/utils/date-formatting"; import { + createApiKeysAction, deleteApiKeyAction, getApiKeysAction, } from "@/features/profile/profile.action"; @@ -157,20 +158,34 @@ export function ProfileAccount({ user }: ProfileAccountProps) { const { mutate: addApiKey, isPending: isAddingApikey } = useMutation({ mutationFn: async () => { - const result = await authClient.apiKey.create({ - name: apiKeyName || "My API Key", - prefix: "sk_" - }); - if (result?.error) { - throw result.error; + // const permissions = { + // organization: ["read", "read-write"], + // } + // + // const result = await authClient.apiKey.create({ + // name: apiKeyName || "My API Key", + // prefix: "sk_", + // permissions + // }); + // + // if (result?.error) { + // throw result.error; + // } + + const result = await createApiKeysAction({ + name: apiKeyName || "My API Key" + }); + console.log(result); + if (!result?.data?.success) { + throw new Error("Failed to create API Key"); } return result; }, onSuccess: (result: any) => { - setCreatedApiKey(result.data.key); + setCreatedApiKey(result.data.value.key); toast.success("API Key created successfully"); diff --git a/src/features/profile/profile.action.ts b/src/features/profile/profile.action.ts index 44f8717b..8e7adfee 100644 --- a/src/features/profile/profile.action.ts +++ b/src/features/profile/profile.action.ts @@ -5,9 +5,10 @@ import { eq } from "drizzle-orm"; import { ServerActionResult } from "@/types/action-type"; import { z } from "zod"; import { headers } from "next/headers"; -import {auth, deleteApiKey, getApiKeys, getPasskeys, revokePasskey} from "@/lib/auth/auth"; +import {auth, createApiKey, deleteApiKey, getApiKeys, getPasskeys, revokePasskey} from "@/lib/auth/auth"; import { user } from "@/db/schema/02_user"; import {userAction} from "@/lib/safe-actions/actions"; +import {ApiKey} from "@better-auth/api-key"; const UpdateProfileSchema = z.object({ name: z.string().optional(), @@ -76,6 +77,32 @@ export const getApiKeysAction = userAction.action(async (): Promise> => { + try { + const apikey = await createApiKey(parsedInput.name); + return { + success: true, + value: apikey, + actionSuccess: { + message: "apikey_created", + }, + }; + } catch (error) { + return { + success: false, + actionError: { + message: "error_creating_apikeys", + cause: error instanceof Error ? error.message : "Unknown error", + }, + }; + } +}); + const DeleteApiKeySchema = z.object({ id: z.string(), }); diff --git a/src/lib/auth/auth.ts b/src/lib/auth/auth.ts index 78e09673..09cc6a94 100644 --- a/src/lib/auth/auth.ts +++ b/src/lib/auth/auth.ts @@ -669,6 +669,16 @@ export const getApiKeys = async () => { }); }; +export const createApiKey = async (name: string) => { + return await auth.api.createApiKey({ + body: { + name, + prefix: "sk_", + }, + headers: await headers(), + }); +}; + export const deleteApiKey = async (keyId: string) => { await auth.api.deleteApiKey({