Code: Select all
esp_err_t loginWS(httpd_req_t *request) {
JwtService jwtService;
AutenticationModel autenticationModel;
AutenticationService autenticationService;
autenticationModel = autenticationService.loadRegister();
cJSON *jsonWs;
cJSON *params;
std::string token = "";
std::string msg = "";
std::string body = "";
std::string login = "";
std::string password = "";
bool error;
body = _getBody(request); //json received
jsonWs = cJSON_Parse(body.c_str());
login = cJSON_GetObjectItem(jsonWs, "_login")->valuestring;
password = cJSON_GetObjectItem(jsonWs, "_password")->valuestring;
if (login == autenticacaoModel.getLogin() && senha == autenticacaoModel.getPassword()) {
token = jwtService.criarJwt();
msg = "";
error = false;
} else {
token = "";
msg = "Login or password invalid";
error = true;
}
cJSON_AddItemToObject(jsonWs = cJSON_CreateObject(), "token", params = cJSON_CreateObject());
cJSON_AddItemToObject(params, "token", cJSON_CreateString(token.c_str()));
cJSON_AddItemToObject(jsonWs, "message", params = cJSON_CreateObject());
cJSON_AddItemToObject(params, "error", cJSON_CreateBool(erro));
cJSON_AddItemToObject(params, "msg", cJSON_CreateString(mensagem.c_str()));
const char *texto = cJSON_Print(jsonWs);
cJSON_Delete(jsonWs);
httpd_resp_set_hdr(request, "Content-Type", "application/json");
httpd_resp_sendstr(request, texto);
free((void *)texto);
return ESP_OK;
}