feat: added the api key list, delete, create in profile modal.

This commit is contained in:
charles-gauthereau
2026-05-25 13:04:42 +02:00
parent 72ecd7f054
commit 0316fdc0a3
3 changed files with 60 additions and 8 deletions
+22 -7
View File
@@ -40,6 +40,7 @@ import { Label } from "@/components/ui/label";
import { useState } from "react"; import { useState } from "react";
import { timeAgo } from "@/utils/date-formatting"; import { timeAgo } from "@/utils/date-formatting";
import { import {
createApiKeysAction,
deleteApiKeyAction, deleteApiKeyAction,
getApiKeysAction, getApiKeysAction,
} from "@/features/profile/profile.action"; } from "@/features/profile/profile.action";
@@ -157,20 +158,34 @@ export function ProfileAccount({ user }: ProfileAccountProps) {
const { mutate: addApiKey, isPending: isAddingApikey } = useMutation({ const { mutate: addApiKey, isPending: isAddingApikey } = useMutation({
mutationFn: async () => { mutationFn: async () => {
const result = await authClient.apiKey.create({
name: apiKeyName || "My API Key",
prefix: "sk_"
});
if (result?.error) { // const permissions = {
throw result.error; // 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; return result;
}, },
onSuccess: (result: any) => { onSuccess: (result: any) => {
setCreatedApiKey(result.data.key); setCreatedApiKey(result.data.value.key);
toast.success("API Key created successfully"); toast.success("API Key created successfully");
+28 -1
View File
@@ -5,9 +5,10 @@ import { eq } from "drizzle-orm";
import { ServerActionResult } from "@/types/action-type"; import { ServerActionResult } from "@/types/action-type";
import { z } from "zod"; import { z } from "zod";
import { headers } from "next/headers"; 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 { user } from "@/db/schema/02_user";
import {userAction} from "@/lib/safe-actions/actions"; import {userAction} from "@/lib/safe-actions/actions";
import {ApiKey} from "@better-auth/api-key";
const UpdateProfileSchema = z.object({ const UpdateProfileSchema = z.object({
name: z.string().optional(), name: z.string().optional(),
@@ -76,6 +77,32 @@ export const getApiKeysAction = userAction.action(async (): Promise<ServerAction
} }
}); });
const CreateApiKeySchema = z.object({
name: z.string(),
});
export const createApiKeysAction = userAction.schema(CreateApiKeySchema).action(async ({parsedInput} ): Promise<ServerActionResult<ApiKey>> => {
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({ const DeleteApiKeySchema = z.object({
id: z.string(), id: z.string(),
}); });
+10
View File
@@ -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) => { export const deleteApiKey = async (keyId: string) => {
await auth.api.deleteApiKey({ await auth.api.deleteApiKey({