From 399666e2469bf8b1df3d013b41c81d998c901a93 Mon Sep 17 00:00:00 2001 From: Yashwin Date: Wed, 8 Jul 2026 17:05:20 +0530 Subject: [PATCH] fix: add clipboard copy fallback for non-secure contexts in API key modals --- .../superadmin/ApikeyReissueModal.tsx | 19 ++++++++++++++++++- src/components/superadmin/NewModuleModal.tsx | 19 ++++++++++++++++++- 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/src/components/superadmin/ApikeyReissueModal.tsx b/src/components/superadmin/ApikeyReissueModal.tsx index d4450d1..bec0cda 100644 --- a/src/components/superadmin/ApikeyReissueModal.tsx +++ b/src/components/superadmin/ApikeyReissueModal.tsx @@ -88,7 +88,24 @@ export const ApikeyReissueModal = ({ const handleCopyApiKey = async (): Promise => { if (apiKey) { try { - await navigator.clipboard.writeText(apiKey); + if (navigator.clipboard && window.isSecureContext) { + await navigator.clipboard.writeText(apiKey); + } else { + // Fallback for non-secure contexts (e.g., HTTP IP/Domain testing) + const textArea = document.createElement('textarea'); + textArea.value = apiKey; + textArea.style.top = '0'; + textArea.style.left = '0'; + textArea.style.position = 'fixed'; + document.body.appendChild(textArea); + textArea.focus(); + textArea.select(); + const successful = document.execCommand('copy'); + document.body.removeChild(textArea); + if (!successful) { + throw new Error('Fallback copy failed'); + } + } setCopied(true); setTimeout(() => setCopied(false), 2000); showToast.success('API key copied to clipboard'); diff --git a/src/components/superadmin/NewModuleModal.tsx b/src/components/superadmin/NewModuleModal.tsx index caaf84d..3b3956e 100644 --- a/src/components/superadmin/NewModuleModal.tsx +++ b/src/components/superadmin/NewModuleModal.tsx @@ -292,7 +292,24 @@ export const NewModuleModal = ({ const handleCopyApiKey = async (): Promise => { if (apiKey) { try { - await navigator.clipboard.writeText(apiKey); + if (navigator.clipboard && window.isSecureContext) { + await navigator.clipboard.writeText(apiKey); + } else { + // Fallback for non-secure contexts (e.g., HTTP IP/Domain testing) + const textArea = document.createElement("textarea"); + textArea.value = apiKey; + textArea.style.top = "0"; + textArea.style.left = "0"; + textArea.style.position = "fixed"; + document.body.appendChild(textArea); + textArea.focus(); + textArea.select(); + const successful = document.execCommand("copy"); + document.body.removeChild(textArea); + if (!successful) { + throw new Error("Fallback copy failed"); + } + } setCopied(true); setTimeout(() => setCopied(false), 2000); showToast.success("API key copied to clipboard");