Optimized check if a loggin session exists
This commit is contained in:
26
src/main.cpp
26
src/main.cpp
@ -259,16 +259,13 @@ int main(int argc, char *argv[]) {
|
|||||||
CROW_ROUTE(app, "/freelancer/logout")
|
CROW_ROUTE(app, "/freelancer/logout")
|
||||||
([&, configuration](const crow::request& getRequest, crow::response& res) {
|
([&, configuration](const crow::request& getRequest, crow::response& res) {
|
||||||
auto& ctx = app.get_context<crow::CookieParser>(getRequest);
|
auto& ctx = app.get_context<crow::CookieParser>(getRequest);
|
||||||
pqxx::connection databaseConnection(configuration.databaseConnectionString);
|
if (Utilities::checkCookieLoginState(configuration, ctx)) {
|
||||||
string loginKey = ctx.get_cookie("loginKey");
|
std::string freelancerEmail = ctx.get_cookie("freelancerEmail");
|
||||||
string freelancerEmail = ctx.get_cookie("freelancerEmail");
|
pqxx::connection databaseConnection(configuration.databaseConnectionString);
|
||||||
if (!freelancerEmail.empty() && !loginKey.empty()) {
|
Database::prepareStatement(databaseConnection, ID_UPDATE_LOGIN_VALIDATION_KEY);
|
||||||
if (Utilities::checkFreelancerLoginState(configuration, loginKey, freelancerEmail)) {
|
Database::executePreparedStatement_UPDATE_LOGIN_VALIDATION_KEY(databaseConnection, "EXPIRED", freelancerEmail);
|
||||||
Database::prepareStatement(databaseConnection, ID_UPDATE_LOGIN_VALIDATION_KEY);
|
ctx.set_cookie("loginKey", Utilities::generateExpiredCookie());
|
||||||
Database::executePreparedStatement_UPDATE_LOGIN_VALIDATION_KEY(databaseConnection, "EXPIRED", freelancerEmail);
|
ctx.set_cookie("freelancerEmail", Utilities::generateExpiredCookie());
|
||||||
ctx.set_cookie("loginKey", Utilities::generateExpiredCookie());
|
|
||||||
ctx.set_cookie("freelancerEmail", Utilities::generateExpiredCookie());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
res.redirect("/");
|
res.redirect("/");
|
||||||
res.end();
|
res.end();
|
||||||
@ -281,13 +278,10 @@ int main(int argc, char *argv[]) {
|
|||||||
CROW_ROUTE(app, "/freelancer/login")
|
CROW_ROUTE(app, "/freelancer/login")
|
||||||
([&,configuration](const crow::request& getRequest ) {
|
([&,configuration](const crow::request& getRequest ) {
|
||||||
auto& ctx = app.get_context<crow::CookieParser>(getRequest);
|
auto& ctx = app.get_context<crow::CookieParser>(getRequest);
|
||||||
string loginKey = ctx.get_cookie("loginKey");
|
|
||||||
string freelancerEmail = ctx.get_cookie("freelancerEmail");
|
|
||||||
string templateHTML = "freelancer_Login.html";
|
string templateHTML = "freelancer_Login.html";
|
||||||
if (!freelancerEmail.empty() && !loginKey.empty()) {
|
if (Utilities::checkCookieLoginState(configuration, ctx))
|
||||||
if (Utilities::checkFreelancerLoginState(configuration, loginKey, freelancerEmail))
|
templateHTML = "freelancer_Redirect_Profile.html";
|
||||||
templateHTML = "freelancer_Redirect_Profile.html";
|
|
||||||
}
|
|
||||||
auto page = crow::mustache::load(templateHTML);
|
auto page = crow::mustache::load(templateHTML);
|
||||||
return page.render();
|
return page.render();
|
||||||
});
|
});
|
||||||
|
@ -424,4 +424,20 @@ namespace Utilities {
|
|||||||
std::string generateExpiredCookie() {
|
std::string generateExpiredCookie() {
|
||||||
return "EXPIRED; HttpOnly; Secure; Path=/; Max-Age=0";
|
return "EXPIRED; HttpOnly; Secure; Path=/; Max-Age=0";
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
/*
|
||||||
|
* checks if the given cookiecontext contains a valid logged in cookie
|
||||||
|
* takes configuration and crow::CookieParser::context
|
||||||
|
*/
|
||||||
|
bool checkCookieLoginState(const Utilities::config& configuration, const crow::CookieParser::context& ctx) {
|
||||||
|
bool loginValid = false;
|
||||||
|
std::string loginKey = ctx.get_cookie("loginKey");
|
||||||
|
std::string freelancerEmail = ctx.get_cookie("freelancerEmail");
|
||||||
|
if (!freelancerEmail.empty() && !loginKey.empty())
|
||||||
|
if (Utilities::checkFreelancerLoginState(configuration, loginKey, freelancerEmail))
|
||||||
|
loginValid = true;
|
||||||
|
|
||||||
|
return loginValid;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#endif
|
Reference in New Issue
Block a user