[captive_portal] Fix captive portal inaccessible when web_server auth is configured (#14734)

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
This commit is contained in:
J. Nick Koston
2026-03-12 14:48:29 -10:00
committed by GitHub
parent 2ca13972b9
commit e15b19b223
3 changed files with 14 additions and 2 deletions

View File

@@ -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();

View File

@@ -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);

View File

@@ -122,6 +122,14 @@ class WebServerBase {
#endif
void add_handler(AsyncWebHandler *handler);
/**
* WARNING: Registers a handler that bypasses the USE_WEBSERVER_AUTH middleware.
*
* This should only be used for endpoints that are intentionally unauthenticated
* (for example, captive portal or very limited-status endpoints). For normal
* endpoints that should respect web server authentication, use add_handler().
*/
void add_handler_without_auth(AsyncWebHandler *handler);
void set_port(uint16_t port) { port_ = port; }
uint16_t get_port() const { return port_; }