Login only once per UniFiController instance to avoid rate limiting

createVoucher() calls apiRequest() twice (cmd/hotspot + stat/voucher),
which previously triggered two separate login requests in rapid succession.
The UniFi controller was rate-limiting the second attempt with 403.

Fix: $loggedIn flag ensures login() is a no-op after the first successful
authentication, reusing the existing session cookie for all API calls.

https://claude.ai/code/session_01UsuvFAmmeagtQa14QA4iaq
This commit is contained in:
Claude 2026-04-22 05:52:45 +00:00
parent 897392041a
commit e0a999b27b
No known key found for this signature in database

View file

@ -7,6 +7,7 @@ class UniFiController {
private $cookieFile;
private $csrfToken = null;
private $sessionCookie = null;
private $loggedIn = false;
public function __construct($controllerUrl, $username, $password, $siteId) {
$this->controllerUrl = rtrim($controllerUrl, '/');
@ -24,6 +25,10 @@ class UniFiController {
// Login zum Controller
private function login() {
if ($this->loggedIn) {
return true;
}
$ch = curl_init();
curl_setopt_array($ch, [
@ -90,6 +95,7 @@ class UniFiController {
$this->csrfToken = $this->readCsrfFromCookieFile();
}
$this->loggedIn = true;
return true;
}