Skip to content

Commit

Permalink
First try in connecting database with NetworkConnection.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
Binchi Zhang committed Nov 30, 2023
1 parent a64ac33 commit 163935d
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 12 deletions.
43 changes: 32 additions & 11 deletions AirClip/NetworkConnection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,17 @@
*/

#include "NetworkConnection.h"
#include "Device.h"
#include "UserManager.h"
#include "ClipboardHelper.h"

const unsigned short NetworkConnection::PORT = 48000;

void NetworkConnection::startServer() {
std::cout << "Starting HTTP API" << std::endl;

crow::SimpleApp app; // Define the crow application
UserManager userManager = UserManager();

CROW_ROUTE(app, "/api/")([]() {
return "Welcome to the API";
Expand All @@ -32,64 +36,81 @@ void NetworkConnection::startServer() {
if (!jsonData)
return crow::response(400);

// TODO: Put in Database
std::cout << "Username: " << jsonData["username"].s() << std::endl;
std::cout << "Content: " << jsonData["content"].s() << std::endl;




return crow::response{"Received!"};
});

// TODO: Implement a websocket to listen for clipboard changes from the server
// https://stackoverflow.com/a/12442805
// https://crowcpp.org/master/guides/websockets/

std::string clipboardContent = "Test data 123";
// std::string clipboardContent = "Test data 123";

// Used to send clipboard data to the client
// TODO: Improve, add authentication so this can't be spoofed
CROW_ROUTE(app, "/api/clipboard/receive")
.methods("GET"_method)([&clipboardContent] {
.methods("GET"_method)([&userManager] {
// Create a crow::json::wvalue object
crow::json::wvalue responseJson;


// TODO: Put in Database
// Add a key-value pair to the JSON object
responseJson["content"] = clipboardContent;
// responseJson["content"] = clipboardContent;

// std::string userID = userManager.findUser(jsonData["username"].s());
// ClipboardHelper::getClipboardEntries(userID);

// Return the JSON object as the response
return responseJson;
});

// Used login a user into the server
CROW_ROUTE(app, "/api/auth/login")
.methods("POST"_method)([](const crow::request &req) {
.methods("POST"_method)([&userManager](const crow::request &req) {
std::cout << "Raw body: " << req.body << std::endl;

auto jsonData = crow::json::load(req.body);

if (!jsonData)
return crow::response(400);

std::cout << "Username: " << jsonData["username"].s() << std::endl;
std::cout << "Password: " << jsonData["password"].s() << std::endl;

// TODO: Login user
if (userManager.authenticateUser( jsonData["username"].s(),jsonData["password"].s())){
userManager.finishUserLogIn(userManager.findUser(jsonData["username"].s()), "", jsonData["username"].s());
return crow::response{"Logged in!"}; // TODO: Change to something better? UserID? WtConnectionID?
//TODO: return Login Info Over Here
}


return crow::response{"Logged in!"}; // TODO: Change to something better? UserID? WtConnectionID?
return crow::response{"Logged in!"}; // TODO: Change to something better? UserID? WtConnectionID?
});

// Used register a user in the server
CROW_ROUTE(app, "/api/auth/register")
.methods("POST"_method)([](const crow::request &req) {
.methods("POST"_method)([&userManager](const crow::request &req) {
std::cout << "Raw body: " << req.body << std::endl;

auto jsonData = crow::json::load(req.body);

if (!jsonData)
return crow::response(400);

std::cout << "Username: " << jsonData["username"].s() << std::endl;
std::cout << "Password: " << jsonData["password"].s() << std::endl;

// TODO: Register user
std::string userID = userManager.registerUser(jsonData["username"].s(), jsonData["password"].s());
if (userID.compare("") != 0){
return crow::response{"Registered!"};
} else {
// TODO: Implement Not Registered Logic Here
}


return crow::response{"Registered!"}; // TODO: Change to something better? UserID? WtConnectionID?
});
Expand Down
2 changes: 2 additions & 0 deletions AirClip/UserManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,8 @@ Device* UserManager::finishUserLogIn(const std::string &userID, const std::strin
// Create a new user with the given userID, username and full name
auto newUser = new User(userID, username, password);

activeUsers.emplace_back(newUser);

// Connect the device to the user
return newUser->connectDevice();
}
2 changes: 1 addition & 1 deletion AirClip/UserManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

class UserManager {
private:
std::vector<User> activeUsers;
std::vector<User*> activeUsers;
DatabaseController *dbc; // Store a reference to the database controller singleton

public:
Expand Down

0 comments on commit 163935d

Please sign in to comment.