From 40b249778e85386bd455d33c09ad8d21be63ac85 Mon Sep 17 00:00:00 2001 From: Matthew Frost Date: Tue, 13 Jun 2023 22:54:27 +0200 Subject: [PATCH] webserver --- include/mainwebserver.h | 33 +++++++++++++++++++++++++-------- 1 file changed, 25 insertions(+), 8 deletions(-) diff --git a/include/mainwebserver.h b/include/mainwebserver.h index 66a23a3..a1cfa21 100644 --- a/include/mainwebserver.h +++ b/include/mainwebserver.h @@ -11,6 +11,7 @@ #include "ESPAsyncWebServer.h" #include "SPIFFS.h" #include "hardware.h" +#include "settings.h" class MainWebServerClass{ @@ -40,6 +41,11 @@ class MainWebServerClass{ }); _server->onNotFound([&](AsyncWebServerRequest *request){ + if(_authRequired){ + if(!request->authenticate(_username.c_str(), _password.c_str())){ + return request->requestAuthentication(); + } + } request->send(200, "application/json", "{\"msg\":\"The content you are looking for was not found\"}"); }); @@ -86,25 +92,36 @@ class MainWebServerClass{ }); #ifdef RELAY1 - _server->on("/state/relay1", HTTP_GET, [] (AsyncWebServerRequest *request) { - if(!request->authenticate(http_username, http_password)) - return request->requestAuthentication(); + _server->on("/state/relay1", HTTP_GET, [&] (AsyncWebServerRequest *request) { + if(_authRequired){ + if(!request->authenticate(_username.c_str(), _password.c_str())){ + return request->requestAuthentication(); + } + } AsyncWebServerResponse *response = request->beginResponse(200, "application/json", "{\"state\":\""+stateDoor()+"\"}"); request->send(response); }); #endif - _server->on("/settings/get/DoorDisabled", HTTP_GET, [] (AsyncWebServerRequest *request) { + _server->on("/settings/get/DoorDisabled", HTTP_GET, [&] (AsyncWebServerRequest *request) { + if(_authRequired){ + if(!request->authenticate(_username.c_str(), _password.c_str())){ + return request->requestAuthentication(); + } + } AsyncWebServerResponse *response = request->beginResponse(200, "application/json", "{\"value\":\""+String(settings.DoorDisabled())+"\"}"); request->send(response); }); - _server->on("/settings/set/DoorDisabled", HTTP_GET, [] (AsyncWebServerRequest *request) { + _server->on("/settings/set/DoorDisabled", HTTP_GET, [&] (AsyncWebServerRequest *request) { String value; - if(!request->authenticate(http_username, http_password)) - return request->requestAuthentication(); - + if(_authRequired){ + if(!request->authenticate(_username.c_str(), _password.c_str())){ + return request->requestAuthentication(); + } + } + if (request->hasParam("value")) { value = request->getParam("value")->value(); if (value == "0") {