-
Notifications
You must be signed in to change notification settings - Fork 402
chore(clerk-js): Remove secret key column in API keys component #7107
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 34 commits
5a9b906
19a8085
6587f25
de27fdb
48d7896
817b8b5
35016e7
b4a8f1c
0407380
c68a03b
996c579
51e5a68
4fb8000
def5944
5ec60e9
03a3651
17bbb98
d6fa9bc
ca40895
9209f76
a0838b1
be632c4
b5254e8
4aeab7b
b0d3251
702f5a9
c43295b
6fceef4
9b30479
f352921
f223828
f7f4549
e8992ae
6537f6e
020398f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "@clerk/localizations": minor | ||
| --- | ||
|
|
||
| Added localization entry for the API key copy modal component. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "@clerk/clerk-js": minor | ||
| --- | ||
|
|
||
| Replaced the persistent key column in the API keys table with a one-time modal that displays the secret immediately after creation. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| import React from 'react'; | ||
|
|
||
| import { Modal } from '@/ui/elements/Modal'; | ||
| import type { ThemableCssProp } from '@/ui/styledSystem'; | ||
|
|
||
| type ApiKeyModalProps = React.ComponentProps<typeof Modal> & { | ||
| modalRoot?: React.MutableRefObject<HTMLElement | null>; | ||
| }; | ||
|
|
||
| /** | ||
| * Container styles for modals rendered within a custom portal root (e.g., UserProfile or OrganizationProfile). | ||
| * When a modalRoot is provided, the modal is scoped to that container rather than the document root, | ||
| * requiring different positioning (absolute instead of fixed) and backdrop styling. | ||
| */ | ||
| const getScopedPortalContainerStyles = (modalRoot?: React.MutableRefObject<HTMLElement | null>): ThemableCssProp => { | ||
| return [ | ||
| { alignItems: 'center' }, | ||
| modalRoot | ||
| ? t => ({ | ||
| position: 'absolute', | ||
| right: 0, | ||
| bottom: 0, | ||
| backgroundColor: 'inherit', | ||
| backdropFilter: `blur(${t.sizes.$2})`, | ||
| display: 'flex', | ||
| justifyContent: 'center', | ||
| minHeight: '100%', | ||
| height: '100%', | ||
| width: '100%', | ||
| borderRadius: t.radii.$lg, | ||
| }) | ||
| : {}, | ||
| ]; | ||
| }; | ||
|
|
||
| export const ApiKeyModal = ({ modalRoot, containerSx, ...modalProps }: ApiKeyModalProps) => { | ||
| return ( | ||
| <Modal | ||
| {...modalProps} | ||
| portalRoot={modalRoot} | ||
| containerSx={[getScopedPortalContainerStyles(modalRoot), containerSx]} | ||
| /> | ||
| ); | ||
| }; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,6 +2,7 @@ import { isClerkAPIResponseError } from '@clerk/shared/error'; | |
| import { useClerk, useOrganization, useUser } from '@clerk/shared/react'; | ||
| import type { CreateAPIKeyParams } from '@clerk/shared/types'; | ||
| import { lazy, useState } from 'react'; | ||
| import { useSWRConfig } from 'swr'; | ||
| import useSWRMutation from 'swr/mutation'; | ||
|
|
||
| import { useProtect } from '@/ui/common'; | ||
|
|
@@ -42,6 +43,12 @@ const RevokeAPIKeyConfirmationModal = lazy(() => | |
| })), | ||
| ); | ||
|
|
||
| const CopyApiKeyModal = lazy(() => | ||
| import(/* webpackChunkName: "copy-api-key-modal"*/ './CopyApiKeyModal').then(module => ({ | ||
| default: module.CopyApiKeyModal, | ||
| })), | ||
| ); | ||
|
|
||
| export const APIKeysPage = ({ subject, perPage, revokeModalRoot }: APIKeysPageProps) => { | ||
| const isOrg = isOrganizationId(subject); | ||
| const canReadAPIKeys = useProtect({ permission: 'org:sys_api_keys:read' }); | ||
|
|
@@ -61,23 +68,34 @@ export const APIKeysPage = ({ subject, perPage, revokeModalRoot }: APIKeysPagePr | |
| cacheKey, | ||
| } = useApiKeys({ subject, perPage, enabled: isOrg ? canReadAPIKeys : true }); | ||
| const card = useCardState(); | ||
| const { trigger: createApiKey, isMutating } = useSWRMutation(cacheKey, (_, { arg }: { arg: CreateAPIKeyParams }) => | ||
| clerk.apiKeys.create(arg), | ||
| const clerk = useClerk(); | ||
| const { | ||
| data: createdApiKey, | ||
| trigger: createApiKey, | ||
| isMutating, | ||
| } = useSWRMutation( | ||
| { | ||
| ...cacheKey, | ||
| action: 'create', | ||
| }, | ||
| (_key, { arg }: { arg: CreateAPIKeyParams }) => clerk.apiKeys.create(arg), | ||
| ); | ||
| const { mutate: mutateApiKeys } = useSWRConfig(); | ||
| const { t } = useLocalizations(); | ||
| const clerk = useClerk(); | ||
| const [isRevokeModalOpen, setIsRevokeModalOpen] = useState(false); | ||
| const [selectedApiKeyId, setSelectedApiKeyId] = useState(''); | ||
| const [selectedApiKeyName, setSelectedApiKeyName] = useState(''); | ||
| const [isCopyModalOpen, setIsCopyModalOpen] = useState(false); | ||
|
|
||
| const handleCreateApiKey = async (params: OnCreateParams, closeCardFn: () => void) => { | ||
| const handleCreateApiKey = async (params: OnCreateParams) => { | ||
| try { | ||
| await createApiKey({ | ||
| ...params, | ||
| subject, | ||
| }); | ||
| closeCardFn(); | ||
| void mutateApiKeys(cacheKey); | ||
| card.setError(undefined); | ||
| setIsCopyModalOpen(true); | ||
| } catch (err: any) { | ||
|
Comment on lines
+90
to
99
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Don't open the copy modal before the new secret exists After - await createApiKey({
+ const created = await createApiKey({
...params,
subject,
});
- void mutateApiKeys(cacheKey);
- card.setError(undefined);
- setIsCopyModalOpen(true);
+ void mutateApiKeys(cacheKey);
+ card.setError(undefined);
+ if (created?.secret) {
+ setLatestCreatedApiKey(created);
+ setIsCopyModalOpen(true);
+ }Add a
🤖 Prompt for AI Agents |
||
| if (isClerkAPIResponseError(err)) { | ||
| if (err.status === 409) { | ||
|
|
@@ -147,7 +165,17 @@ export const APIKeysPage = ({ subject, perPage, revokeModalRoot }: APIKeysPagePr | |
| </Action.Card> | ||
| </Flex> | ||
| </Action.Open> | ||
|
|
||
| <CopyApiKeyModal | ||
| isOpen={isCopyModalOpen} | ||
| onOpen={() => setIsCopyModalOpen(true)} | ||
| onClose={() => setIsCopyModalOpen(false)} | ||
| apiKeyName={createdApiKey?.name ?? ''} | ||
| apiKeySecret={createdApiKey?.secret ?? ''} | ||
| modalRoot={revokeModalRoot} | ||
| /> | ||
| </Action.Root> | ||
|
|
||
| <ApiKeysTable | ||
| rows={apiKeys} | ||
| isLoading={isLoading} | ||
|
|
@@ -164,6 +192,8 @@ export const APIKeysPage = ({ subject, perPage, revokeModalRoot }: APIKeysPagePr | |
| rowInfo={{ allRowsCount: itemCount, startingRow, endingRow }} | ||
| /> | ||
| )} | ||
|
|
||
| {/* Modals */} | ||
| <RevokeAPIKeyConfirmationModal | ||
| subject={subject} | ||
| isOpen={isRevokeModalOpen} | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.