[captive_portal] Fix captive portal inaccessible when web_server auth is configured

Closes https://github.com/esphome/esphome/issues/12710
This commit is contained in:
J. Nick Koston
2026-03-12 10:06:07 -10:00
parent 05d285ba86
commit b20a2a12a8
3 changed files with 7 additions and 2 deletions
@@ -61,7 +61,7 @@ void CaptivePortal::handle_wifisave(AsyncWebServerRequest *request) {
// Defer save to main loop thread to avoid NVS operations from HTTP thread
this->defer([ssid, psk]() { wifi::global_wifi_component->save_wifi_sta(ssid.c_str(), psk.c_str()); });
#endif
request->redirect(ESPHOME_F("/?save"));
request->send(200, ESPHOME_F("text/plain"), ESPHOME_F("Saved. Connecting..."));
}
void CaptivePortal::setup() {
@@ -71,7 +71,7 @@ void CaptivePortal::setup() {
void CaptivePortal::start() {
this->base_->init();
if (!this->initialized_) {
this->base_->add_handler(this);
this->base_->add_handler_without_auth(this);
}
network::IPAddress ip = wifi::global_wifi_component->wifi_soft_ap_ip();
@@ -11,6 +11,10 @@ void WebServerBase::add_handler(AsyncWebHandler *handler) {
handler = new internal::AuthMiddlewareHandler(handler, &credentials_);
}
#endif
this->add_handler_without_auth(handler);
}
void WebServerBase::add_handler_without_auth(AsyncWebHandler *handler) {
this->handlers_.push_back(handler);
if (this->server_ != nullptr) {
this->server_->addHandler(handler);
@@ -122,6 +122,7 @@ class WebServerBase {
#endif
void add_handler(AsyncWebHandler *handler);
void add_handler_without_auth(AsyncWebHandler *handler);
void set_port(uint16_t port) { port_ = port; }
uint16_t get_port() const { return port_; }