Skip to content

Commit a4a1dc9

Browse files
authored
fix(admin): fix Copy API Key behavior in secret key created dialog(#15529) (#15531)
fixes #15529 ## What Fixes the copy behavior in the "Secret key created" dialog for secret API keys. ## Why Copy could report success even when clipboard write failed (or when token was missing), causing users to paste incorrect content. ## How - Added early return when `createdKey.token` is missing. - Wrapped clipboard write in `try/catch`. - Added secure-context and clipboard availability checks. - Show success toast only on successful copy, failure toast on errors. ## Testing - Created a secret key in Admin. - Clicked `Copy API Key` and verified token is copied. - Verified failure toast appears when clipboard API is unavailable. closes #15529
1 parent 68f7a0e commit a4a1dc9

2 files changed

Lines changed: 21 additions & 5 deletions

File tree

.changeset/happy-cows-roll.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@medusajs/dashboard": patch
3+
---
4+
5+
fix(dashboard): fix Copy API Key behavior in secret key created dialog

packages/admin/dashboard/src/routes/api-key-management/api-key-management-create/components/api-key-create-form/api-key-create-form.tsx

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -78,13 +78,22 @@ export const ApiKeyCreateForm = ({ keyType }: ApiKeyCreateFormProps) => {
7878
)
7979
})
8080

81-
const handleCopyToken = () => {
82-
if (!createdKey) {
81+
const handleCopyToken = async () => {
82+
if (!createdKey?.token) {
8383
toast.error(t("apiKeyManagement.create.copySecretTokenFailure"))
84+
return
8485
}
8586

86-
navigator.clipboard.writeText(createdKey?.token ?? "")
87-
toast.success(t("apiKeyManagement.create.copySecretTokenSuccess"))
87+
try {
88+
if (!window.isSecureContext || !navigator.clipboard?.writeText) {
89+
throw new Error("Clipboard API unavailable")
90+
}
91+
92+
await navigator.clipboard.writeText(createdKey.token)
93+
toast.success(t("apiKeyManagement.create.copySecretTokenSuccess"))
94+
} catch {
95+
toast.error(t("apiKeyManagement.create.copySecretTokenFailure"))
96+
}
8897
}
8998

9099
const handleGoToSecretKey = () => {
@@ -188,7 +197,9 @@ export const ApiKeyCreateForm = ({ keyType }: ApiKeyCreateFormProps) => {
188197
variant="secondary"
189198
type="button"
190199
className="w-full"
191-
onClick={handleCopyToken}
200+
onClick={() => {
201+
void handleCopyToken()
202+
}}
192203
>
193204
{t("apiKeyManagement.actions.copy")}
194205
</Button>

0 commit comments

Comments
 (0)