RFID message ins backend geht, websocket fürs frontend
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
#pragma once
|
||||
#include <Arduino.h>
|
||||
#include <HTTPClient.h>
|
||||
#include "master.h"
|
||||
@@ -17,34 +18,76 @@ bool backendOnline() {
|
||||
http.addHeader("Authorization", String("Bearer ") + BACKEND_TOKEN);
|
||||
|
||||
int httpCode = http.GET();
|
||||
bool isOnline = (httpCode == HTTP_CODE_OK);
|
||||
|
||||
if (httpCode == HTTP_CODE_OK) {
|
||||
return true;
|
||||
if (isOnline) {
|
||||
Serial.println("Database server connection successful");
|
||||
} else {
|
||||
return false;
|
||||
Serial.printf("Database server connection failed, error: %d\n", httpCode);
|
||||
}
|
||||
|
||||
http.end();
|
||||
return isOnline;
|
||||
}
|
||||
|
||||
bool userExists(const String& userId) {
|
||||
struct UserData {
|
||||
String uid;
|
||||
String firstname;
|
||||
String lastname;
|
||||
int alter;
|
||||
bool exists;
|
||||
};
|
||||
|
||||
// UserData checkUser(const String& uid) is defined only once to avoid redefinition errors.
|
||||
UserData checkUser(const String& uid) {
|
||||
|
||||
UserData userData = {"", "", "", 0, false};
|
||||
|
||||
if (!backendOnline()) {
|
||||
Serial.println("No internet connection, cannot check user existence.");
|
||||
return false;
|
||||
Serial.println("No internet connection, cannot check user.");
|
||||
return userData;
|
||||
}
|
||||
|
||||
HTTPClient http;
|
||||
http.begin(String(BACKEND_SERVER) + "/api/users/" + userId);
|
||||
http.begin(String(BACKEND_SERVER) + "/api/users/find");
|
||||
http.addHeader("Content-Type", "application/json");
|
||||
http.addHeader("Authorization", String("Bearer ") + BACKEND_TOKEN);
|
||||
|
||||
//Post request to check if user exists
|
||||
int httpCode = http.POST("");
|
||||
// Create JSON payload
|
||||
StaticJsonDocument<200> requestDoc;
|
||||
requestDoc["uid"] = uid;
|
||||
String requestBody;
|
||||
serializeJson(requestDoc, requestBody);
|
||||
|
||||
int httpCode = http.POST(requestBody);
|
||||
|
||||
if (httpCode == HTTP_CODE_OK) {
|
||||
String payload = http.getString();
|
||||
StaticJsonDocument<512> responseDoc;
|
||||
DeserializationError error = deserializeJson(responseDoc, payload);
|
||||
|
||||
if (!error) {
|
||||
userData.uid = responseDoc["uid"].as<String>();
|
||||
userData.firstname = responseDoc["firstname"].as<String>();
|
||||
userData.lastname = responseDoc["lastname"].as<String>();
|
||||
userData.alter = responseDoc["alter"] | 0;
|
||||
userData.exists = true;
|
||||
}
|
||||
} else {
|
||||
Serial.printf("User check failed, HTTP code: %d\n", httpCode);
|
||||
}
|
||||
|
||||
http.end();
|
||||
return userData;
|
||||
}
|
||||
|
||||
// Keep this for backward compatibility
|
||||
bool userExists(const String& uid) {
|
||||
return checkUser(uid).exists;
|
||||
}
|
||||
|
||||
void setupBackendRoutes(AsyncWebServer& server) {
|
||||
|
||||
server.on("/api/health", HTTP_GET, [](AsyncWebServerRequest *request) {
|
||||
|
||||
DynamicJsonDocument doc(64);
|
||||
|
||||
Reference in New Issue
Block a user