| 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 | export function OrganizationApiKeys({ |
| 23 | className, |
| 24 | organizationId, |
| 25 | }: OrganizationApiKeysProps) |
| 26 | const { authClient |
| 27 | const { creatorRole |
| 28 | const memberRole = |
| 29 | query: { organizationId |
| 30 | }); |
| 31 | const isCreator = |
| 32 | const permissionOptions = |
| 33 | enabled: !memberRole.isPending |
| 34 | organizationId, |
| 35 | }; |
| 36 | const canRead = |
| 37 | ...permissionOptions, |
| 38 | permissions: { apiKey: [ |
| 39 | OrganizationAuthClient[" |
| 40 | >[0][ |
| 41 | }); |
| 42 | const canCreate = |
| 43 | ...permissionOptions, |
| 44 | permissions: { apiKey: [ |
| 45 | OrganizationAuthClient[" |
| 46 | >[0][ |
| 47 | }); |
| 48 | const canUpdate = |
| 49 | ...permissionOptions, |
| 50 | permissions: { apiKey: [ |
| 51 | OrganizationAuthClient[" |
| 52 | >[0][ |
| 53 | }); |
| 54 | const canDelete = |
| 55 | ...permissionOptions, |
| 56 | permissions: { apiKey: [ |
| 57 | OrganizationAuthClient[" |
| 58 | >[0][ |
| 59 | }); |
| 60 | const permissionPending = |
| 61 | !isCreator && |
| 62 | !memberRole.isPending && |
| 63 | (canRead.isPending || |
| 64 | canCreate.isPending || |
| 65 | canUpdate.isPending || |
| 66 | canDelete.isPending); |
| 67 | const isPending = |
| 68 | const canReadKeys = |
| 69 | |
| 70 | if (!isPending && |
| 71 | |
| 72 | return ( |
| 73 | <ApiKeys |
| 74 | className={className} |
| 75 | hideCreate={!isCreator |
| 76 | hideDelete={!isCreator |
| 77 | hideUpdate={!isCreator |
| 78 | isPending={isPending} |
| 79 | organizationId={organizationId} |
| 80 | /> |
| 81 | ); |
| 82 | } |