import React, { useState, useEffect, useRef } from 'react'; import { useNavigate } from 'react-router-dom'; import axios from 'axios'; import { API_BASE_URL } from '../config'; export const Login: React.FC = () => { const [sessionId, setSessionId] = useState(''); const [error, setError] = useState(''); const [isLoading, setIsLoading] = useState(false); const [showVnc, setShowVnc] = useState(false); const [vncStatus, setVncStatus] = useState(''); const pollIntervalRef = useRef(null); const navigate = useNavigate(); // Check if already authenticated (ssl login check) useEffect(() => { checkAuth(); return () => { if (pollIntervalRef.current) { clearInterval(pollIntervalRef.current); } }; }, []); const checkAuth = async () => { try { const res = await axios.get(`${API_BASE_URL}/auth/status`); if (res.data.authenticated) { navigate('/'); } } catch (err) { // Not authenticated } }; const getVncUrl = () => { const host = window.location.hostname; // autoconnect=true, resize=remote (server resizing) for full screen mobile return `http://${host}:6080/vnc.html?autoconnect=true&resize=remote&quality=9`; }; const handleVncLogin = async () => { setError(''); setIsLoading(true); setVncStatus('Initializing secure browser...'); try { // Start the VNC session (SSL Login flow) const res = await axios.post(`${API_BASE_URL}/auth/start-vnc`); if (res.data.status === 'started') { setShowVnc(true); setIsLoading(false); setVncStatus('Waiting for login...'); // Poll for completion pollIntervalRef.current = window.setInterval(async () => { try { const checkRes = await axios.get(`${API_BASE_URL}/auth/check-vnc`); if (checkRes.data.logged_in) { clearInterval(pollIntervalRef.current!); setVncStatus('Login successful! Redirecting...'); setTimeout(() => navigate('/'), 1000); } } catch (err) { console.error('VNC check error:', err); } }, 2000); } else { setError(res.data.message || 'Failed to start browser'); setIsLoading(false); } } catch (err: any) { setError(err.response?.data?.detail || 'Failed to start login session'); setIsLoading(false); } }; const handleCancelVnc = async () => { if (pollIntervalRef.current) { clearInterval(pollIntervalRef.current); } try { await axios.post(`${API_BASE_URL}/auth/stop-vnc`); } catch (err) { console.error('Failed to stop VNC:', err); } setShowVnc(false); setVncStatus(''); }; const handleManualLogin = async () => { if (!sessionId.trim()) return; setError(''); setIsLoading(true); try { const res = await axios.post(`${API_BASE_URL}/auth/credentials`, { credentials: { http: { cookies: { sessionid: sessionId.trim() } } } }); if (res.data.status === 'success') { navigate('/'); } else { setError('Invalid session ID.'); } } catch (err: any) { setError('Connection failed.'); } finally { setIsLoading(false); } }; // Full Screen VNC View if (showVnc) { return (
{/* Floating Control Bar */}
Secure Browser Active
{/* Status Toast */} {vncStatus && (
{vncStatus}
)} {/* Full Screen Iframe */}