From bd8d815248421485004ef59ca33af6f204c5287e Mon Sep 17 00:00:00 2001 From: Tina_Azure <-> Date: Wed, 19 Apr 2023 02:14:35 +0200 Subject: [PATCH] handler login fulfilment --- src/main.cpp | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 60 insertions(+), 2 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 1ccc657..84d4e85 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -280,10 +280,68 @@ int main(int argc, char *argv[]) { - /*string templateHTML = "customer_Freelancer_Template_Request.html"; + string templateHTML = "freelancer_Login.html"; auto page = crow::mustache::load(templateHTML); - return page.render();*/ + return page.render(); + }); + + /* + * Page for freelancer to log in fulfillment + */ + CROW_ROUTE(app, "/freelancer/login/fulfilment").methods("POST"_method) + ([databaseURI, configuration](const crow::request& postRequest ) { + crow::mustache::context ctx; + string postRequestBody = postRequest.body; + Utilities::decodeString(postRequestBody); + vector splitPostRequestBody = Utilities::splitStringIntoVector(postRequestBody, '&'); + string email, password; + + for (const string& item : splitPostRequestBody) { + vector splitItem = Utilities::splitStringIntoVector(item, '='); + if (splitItem.at(0) == "freelanceremail") + email = splitItem.at(1); + if (splitItem.at(0) == "freelancerpassword") + password = splitItem.at(1); + } + + //check if login data is complete + if (!email.empty() && !password.empty()){ + //check if freelancer exists + pqxx::connection databaseConnection(databaseURI); + pqxx::result checkFreelancerExists = Database::executePreparedStatement_SELECT_CHECK_EMAIL_EXISTS(databaseConnection, email); + int checkFreelancerExistsExtracted = stoi(checkFreelancerExists.at(0).at(0).c_str()); + if (checkFreelancerExistsExtracted == 1) { + pqxx::result freelancerSalt = Database::executePreparedStatement_SELECT_FREELANCER_SALT(databaseConnection, email); + string salt = freelancerSalt.at(0).at(0).c_str(); + string hash = Utilities::hashPassword(salt, password); + pqxx::result checkFreelancerHash = Database::executePreparedStatement_SELECT_CHECK_HASH_VALID(databaseConnection, email, hash); + int checkFreelancerHashExtracted = stoi(checkFreelancerHash.at(0).at(0).c_str()); + if (checkFreelancerHashExtracted == 1) { + //todo::create secure cookie + ctx["LOGIN_SUCCESS"] = true; + } + else { + ctx["LOGIN_ERROR"] = true; + ctx["LOGIN_ERROR_LOGIN_DATA_INVALID"] = true; + } + } + else { + ctx["LOGIN_ERROR"] = true; + ctx["LOGIN_ERROR_LOGIN_DATA_INVALID"] = true; + } + } + else { + ctx["LOGIN_ERROR"] = true; + ctx["LOGIN_ERROR_EMAIL_PASS_NOT_FILLED"] = true; + } + + + string templateHTML = "freelancer_Login_Fulfilment.html"; + auto page = crow::mustache::load(templateHTML); + + return page.render(ctx); + }); /* * Page for freelancer to sign up