diff --git a/main/service/setting.c b/main/service/setting.c index 7e50aff..cddd0fc 100644 --- a/main/service/setting.c +++ b/main/service/setting.c @@ -177,6 +177,8 @@ static esp_err_t setting_post_handler(httpd_req_t* req) cJSON* vin_climit_item = cJSON_GetObjectItem(root, "vin_current_limit"); cJSON* main_climit_item = cJSON_GetObjectItem(root, "main_current_limit"); cJSON* usb_climit_item = cJSON_GetObjectItem(root, "usb_current_limit"); + cJSON* new_username_item = cJSON_GetObjectItem(root, "new_username"); + cJSON* new_password_item = cJSON_GetObjectItem(root, "new_password"); if (mode_item && cJSON_IsString(mode_item)) { @@ -322,6 +324,17 @@ static esp_err_t setting_post_handler(httpd_req_t* req) } httpd_resp_sendstr(req, "{\"status\":\"current_limit_updated\"}"); } + else if (new_username_item && cJSON_IsString(new_username_item) && new_password_item && + cJSON_IsString(new_password_item)) + { + const char* new_username = new_username_item->valuestring; + const char* new_password = new_password_item->valuestring; + + nconfig_write(PAGE_USERNAME, new_username); + nconfig_write(PAGE_PASSWORD, new_password); + ESP_LOGI(TAG, "Username and password updated successfully."); + httpd_resp_sendstr(req, "{\"status\":\"user_credentials_updated\"}"); + } else { httpd_resp_send_err(req, HTTPD_400_BAD_REQUEST, "Invalid payload"); diff --git a/main/service/webserver.c b/main/service/webserver.c index 9dbfd23..cbca18e 100644 --- a/main/service/webserver.c +++ b/main/service/webserver.c @@ -80,12 +80,58 @@ static esp_err_t login_handler(httpd_req_t* req) return ESP_FAIL; } - const char* username = username_json->valuestring; - const char* password = password_json->valuestring; + const char* received_username = username_json->valuestring; + const char* received_password = password_json->valuestring; - // TODO: Implement actual credential validation - // For now, a simple hardcoded check - if (strcmp(username, "admin") == 0 && strcmp(password, "password") == 0) + // Get stored username and password from nconfig + size_t stored_username_len = 0; + size_t stored_password_len = 0; + char* stored_username = NULL; + char* stored_password = NULL; + bool credentials_match = false; + + if (nconfig_get_str_len(PAGE_USERNAME, &stored_username_len) == ESP_OK && stored_username_len > 1) + { + stored_username = (char*)malloc(stored_username_len); + if (stored_username) + { + if (nconfig_read(PAGE_USERNAME, stored_username, stored_username_len) != ESP_OK) + { + ESP_LOGE(TAG, "Failed to read stored username from nconfig"); + free(stored_username); + stored_username = NULL; + } + } + } + + if (nconfig_get_str_len(PAGE_PASSWORD, &stored_password_len) == ESP_OK && stored_password_len > 1) + { + stored_password = (char*)malloc(stored_password_len); + if (stored_password) + { + if (nconfig_read(PAGE_PASSWORD, stored_password, stored_password_len) != ESP_OK) + { + ESP_LOGE(TAG, "Failed to read stored password from nconfig"); + free(stored_password); + stored_password = NULL; + } + } + } + + if (stored_username && stored_password) + { + if (strcmp(received_username, stored_username) == 0 && strcmp(received_password, stored_password) == 0) + { + credentials_match = true; + } + } + + if (stored_username) + free(stored_username); + if (stored_password) + free(stored_password); + + if (credentials_match) { char* token = auth_generate_token(); if (token) diff --git a/page/index.html b/page/index.html index 8f3d57e..c20adbb 100644 --- a/page/index.html +++ b/page/index.html @@ -197,6 +197,11 @@ id="current-limit-settings-tab" role="tab" type="button">Current Limit +