import React, { useState, useEffect } from 'react'; import { Shield, Copy, Check, AlertTriangle } from 'lucide-react'; import Spinner from '../Spinner'; const BasicAuthModal = ({ environment, prodId, onClose, api }) => { const [loading, setLoading] = useState(true); const [status, setStatus] = useState({ enabled: false, username: null }); const [credentials, setCredentials] = useState(null); const [toggling, setToggling] = useState(false); const [copied, setCopied] = useState(null); useEffect(() => { loadStatus(); }, []); async function loadStatus() { setLoading(true); try { const data = await api.getBasicAuthStatus(prodId, environment.id); setStatus(data); } catch { setStatus({ enabled: false, username: null }); } finally { setLoading(false); } } async function handleEnable() { setToggling(true); try { const data = await api.enableBasicAuth(prodId, environment.id); if (data.success) { setStatus({ enabled: true, username: data.username }); setCredentials({ username: data.username, password: data.password }); } } catch (err) { console.error('Failed to enable basic auth:', err); } finally { setToggling(false); } } async function handleDisable() { setToggling(true); try { const data = await api.disableBasicAuth(prodId, environment.id); if (data.success) { setStatus({ enabled: false, username: null }); setCredentials(null); } } catch (err) { console.error('Failed to disable basic auth:', err); } finally { setToggling(false); } } function copyToClipboard(text, field) { navigator.clipboard.writeText(text); setCopied(field); setTimeout(() => setCopied(null), 2000); } const envName = environment?.name || 'Environment'; return (
e.stopPropagation()}>

Basic Auth

{envName}
{loading ? (
Loading status...
) : ( <>

Adds password protection to the site. Visitors must enter credentials to access any page.

{status.enabled && status.username && !credentials && (
{status.username}

Password was shown when Basic Auth was first enabled. Disable and re-enable to generate new credentials.

)} {credentials && (
Save these credentials now. The password will not be shown again.
{credentials.username}
{credentials.password}
)} )}
); }; export default BasicAuthModal;