Код: Выделить всё
const connectVPN = async () => {
// If already connected, reset timer
if (isConnected) {
resetTimer();
return;
}
setConnecting(true);
// Retry logic up to maxAttempts
for (let i = 0; i < maxAttempts; i++) {
try {
const res = await fetch('http://localhost:8080/api/v1/credentials');
if (!res.ok) throw new Error("Failed to get credentials");
const data = await res.json();
const {username, password} = data;
const success = true;
if (success) {
setIsConnected(true);
setConnectionAttempts(0);
setConnecting(false);
resetTimer();
startTimer();
return;
} else {
throw new Error("VPN connect failed");
}
} catch (err) {
console.log(err);
setConnectionAttempts(i + 1);
}
}
setConnecting(false);
alert(t('error'));
};
Подробнее здесь: https://stackoverflow.com/questions/792 ... -protocols