'use client'; import { useState } from 'react'; import { X, Copy, Check, Server, Globe } from 'lucide-react'; import { Button } from './ui/button'; import { useRegisterModal } from './modal/ModalStackProvider'; interface PublicKeyModalProps { isOpen: boolean; onClose: () => void; publicKey: string; serverName: string; serverIp: string; } export function PublicKeyModal({ isOpen, onClose, publicKey, serverName, serverIp }: PublicKeyModalProps) { useRegisterModal(isOpen, { id: 'public-key-modal', allowEscape: true, onClose }); const [copied, setCopied] = useState(false); const [commandCopied, setCommandCopied] = useState(false); if (!isOpen) return null; const handleCopy = async () => { try { // Try modern clipboard API first if (navigator.clipboard && window.isSecureContext) { await navigator.clipboard.writeText(publicKey); setCopied(true); setTimeout(() => setCopied(false), 2000); } else { // Fallback for older browsers or non-HTTPS const textArea = document.createElement('textarea'); textArea.value = publicKey; textArea.style.position = 'fixed'; textArea.style.left = '-999999px'; textArea.style.top = '-999999px'; document.body.appendChild(textArea); textArea.focus(); textArea.select(); try { document.execCommand('copy'); setCopied(true); setTimeout(() => setCopied(false), 2000); } catch (fallbackError) { console.error('Fallback copy failed:', fallbackError); // If all else fails, show the key in an alert alert('Please manually copy this key:\n\n' + publicKey); } document.body.removeChild(textArea); } } catch (error) { console.error('Failed to copy to clipboard:', error); // Fallback: show the key in an alert alert('Please manually copy this key:\n\n' + publicKey); } }; const handleCopyCommand = async () => { const command = `echo "${publicKey}" >> ~/.ssh/authorized_keys`; try { // Try modern clipboard API first if (navigator.clipboard && window.isSecureContext) { await navigator.clipboard.writeText(command); setCommandCopied(true); setTimeout(() => setCommandCopied(false), 2000); } else { // Fallback for older browsers or non-HTTPS const textArea = document.createElement('textarea'); textArea.value = command; textArea.style.position = 'fixed'; textArea.style.left = '-999999px'; textArea.style.top = '-999999px'; document.body.appendChild(textArea); textArea.focus(); textArea.select(); try { document.execCommand('copy'); setCommandCopied(true); setTimeout(() => setCommandCopied(false), 2000); } catch (fallbackError) { console.error('Fallback copy failed:', fallbackError); alert('Please manually copy this command:\n\n' + command); } document.body.removeChild(textArea); } } catch (error) { console.error('Failed to copy command to clipboard:', error); alert('Please manually copy this command:\n\n' + command); } }; return (
Add this key to your server's authorized_keys
ssh root@{serverIp}echo "<paste-key>" >> ~/.ssh/authorized_keyschmod 600 ~/.ssh/authorized_keys
echo "{publicKey}" >> ~/.ssh/authorized_keys
Copy and paste this command directly into your server terminal to add the key to authorized_keys