-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
78 lines (67 loc) · 2.31 KB
/
main.cpp
File metadata and controls
78 lines (67 loc) · 2.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include <iostream>
#include <fstream>
#include <nlohmann/json.hpp>
#include <crow.h>
#include <optional>
#include <unordered_map>
using json = nlohmann::json;
std::optional<json> load_users(const std::string& filepath) {
try {
std::ifstream users_file(filepath);
if (!users_file.is_open()) {
return std::nullopt;
}
json users;
users_file >> users;
return users;
} catch (const std::exception& e) {
std::cerr << "Error loading JSON: " << e.what() << std::endl;
return std::nullopt;
}
}
std::optional<std::unordered_map<std::string, std::string>> authenticate_user(
const json& users,
const std::string& username,
const std::string& password
) {
if (!users.contains("authors")) {
return std::nullopt;
}
for (const auto& user : users["authors"]) {
if (user["user"] == username && user["password"] == password) {
return std::unordered_map<std::string, std::string>{
{"user", username}
};
}
}
return std::nullopt;
}
int main() {
crow::SimpleApp app;
CROW_ROUTE(app, "/watan/ilovecats")
.methods("GET"_method)
([](const crow::request& req) {
auto user_p = req.url_params.get("user");
auto pass_p = req.url_params.get("password");
if (!user_p || !pass_p) {
return crow::response(400, "Missing parameters");
}
auto users = load_users("db/users.json");
if (!users) {
return crow::response(500, "Error loading user database");
}
auto authenticated_user = authenticate_user(*users, user_p, pass_p);
if (authenticated_user) {
crow::json::wvalue response_json;
response_json["status"] = "success";
response_json["message"] = "Authentication successful";
response_json["user"] = authenticated_user->at("user");
return crow::response(200, response_json);
} else {
return crow::response(401, "Authentication failed");
}
});
uint16_t port = 8080;
app.port(port).multithreaded().run();
return 0;
}