| 1 | "use client"; |
| 2 | |
| 3 | import { |
| 4 | hasMemberRole, |
| 5 | type OrganizationAuthClient, |
| 6 | } from "@better-auth-ui/core/plugins/organization"; |
| 7 | import { useAuth, useAuthPlugin |
| 8 | import { |
| 9 | useActiveMemberRole, |
| 10 | useHasPermission, |
| 11 | } from "@better-auth-ui/react/plugins/organization"; |
| 12 | import { organizationPlugin } |
| 13 | import { ApiKeys } |
| 14 | |
| 15 | export type OrganizationApiKeysProps |
| 16 | className?: string |
| 17 | organizationId: string |
| 18 | organizationSlug: string |
| 19 | }; |
| 20 | |
| 21 | /** |
| 22 | * {@link |
| 23 | * |
| 24 | * Access is resolved per API-key action. The configured organization creator role receives Better Auth's creator override. |
| 25 | */ |
| 26 | export function OrganizationApiKeys({ |
| 27 | className, |
| 28 | organizationId, |
| 29 | }: OrganizationApiKeysProps) |
| 30 | const { authClient |
| 31 | const { creatorRole |
| 32 | const memberRole = |
| 33 | query: { organizationId |
| 34 | }); |
| 35 | const isCreator = |
| 36 | const permissionOptions = |
| 37 | enabled: !memberRole.isPending |
| 38 | organizationId, |
| 39 | }; |
| 40 | const canRead = |
| 41 | ...permissionOptions, |
| 42 | permissions: { apiKey: [ |
| 43 | OrganizationAuthClient[" |
| 44 | >[0][ |
| 45 | }); |
| 46 | const canCreate = |
| 47 | ...permissionOptions, |
| 48 | permissions: { apiKey: [ |
| 49 | OrganizationAuthClient[" |
| 50 | >[0][ |
| 51 | }); |
| 52 | const canUpdate = |
| 53 | ...permissionOptions, |
| 54 | permissions: { apiKey: [ |
| 55 | OrganizationAuthClient[" |
| 56 | >[0][ |
| 57 | }); |
| 58 | const canDelete = |
| 59 | ...permissionOptions, |
| 60 | permissions: { apiKey: [ |
| 61 | OrganizationAuthClient[" |
| 62 | >[0][ |
| 63 | }); |
| 64 | const permissionPending = |
| 65 | !isCreator && |
| 66 | !memberRole.isPending && |
| 67 | (canRead.isPending || |
| 68 | canCreate.isPending || |
| 69 | canUpdate.isPending || |
| 70 | canDelete.isPending); |
| 71 | const isPending = |
| 72 | const canReadKeys = |
| 73 | |
| 74 | if (!isPending && |
| 75 | |
| 76 | return ( |
| 77 | <ApiKeys |
| 78 | className={className} |
| 79 | hideCreate={!isCreator |
| 80 | hideDelete={!isCreator |
| 81 | hideUpdate={!isCreator |
| 82 | isPending={isPending} |
| 83 | organizationId={organizationId} |
| 84 | /> |
| 85 | ); |
| 86 | } |