fix: add clipboard copy fallback for non-secure contexts in API key modals

This commit is contained in:
Yashwin 2026-07-08 17:05:20 +05:30
parent 54d5b978aa
commit 399666e246
2 changed files with 36 additions and 2 deletions

View File

@ -88,7 +88,24 @@ export const ApikeyReissueModal = ({
const handleCopyApiKey = async (): Promise<void> => { const handleCopyApiKey = async (): Promise<void> => {
if (apiKey) { if (apiKey) {
try { try {
if (navigator.clipboard && window.isSecureContext) {
await navigator.clipboard.writeText(apiKey); 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); setCopied(true);
setTimeout(() => setCopied(false), 2000); setTimeout(() => setCopied(false), 2000);
showToast.success('API key copied to clipboard'); showToast.success('API key copied to clipboard');

View File

@ -292,7 +292,24 @@ export const NewModuleModal = ({
const handleCopyApiKey = async (): Promise<void> => { const handleCopyApiKey = async (): Promise<void> => {
if (apiKey) { if (apiKey) {
try { try {
if (navigator.clipboard && window.isSecureContext) {
await navigator.clipboard.writeText(apiKey); 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); setCopied(true);
setTimeout(() => setCopied(false), 2000); setTimeout(() => setCopied(false), 2000);
showToast.success("API key copied to clipboard"); showToast.success("API key copied to clipboard");