Optimized check if a loggin session exists
This commit is contained in:
14
src/main.cpp
14
src/main.cpp
@ -259,17 +259,14 @@ int main(int argc, char *argv[]) {
|
||||
CROW_ROUTE(app, "/freelancer/logout")
|
||||
([&, configuration](const crow::request& getRequest, crow::response& res) {
|
||||
auto& ctx = app.get_context<crow::CookieParser>(getRequest);
|
||||
if (Utilities::checkCookieLoginState(configuration, ctx)) {
|
||||
std::string freelancerEmail = ctx.get_cookie("freelancerEmail");
|
||||
pqxx::connection databaseConnection(configuration.databaseConnectionString);
|
||||
string loginKey = ctx.get_cookie("loginKey");
|
||||
string freelancerEmail = ctx.get_cookie("freelancerEmail");
|
||||
if (!freelancerEmail.empty() && !loginKey.empty()) {
|
||||
if (Utilities::checkFreelancerLoginState(configuration, loginKey, freelancerEmail)) {
|
||||
Database::prepareStatement(databaseConnection, ID_UPDATE_LOGIN_VALIDATION_KEY);
|
||||
Database::executePreparedStatement_UPDATE_LOGIN_VALIDATION_KEY(databaseConnection, "EXPIRED", freelancerEmail);
|
||||
ctx.set_cookie("loginKey", Utilities::generateExpiredCookie());
|
||||
ctx.set_cookie("freelancerEmail", Utilities::generateExpiredCookie());
|
||||
}
|
||||
}
|
||||
res.redirect("/");
|
||||
res.end();
|
||||
});
|
||||
@ -281,13 +278,10 @@ int main(int argc, char *argv[]) {
|
||||
CROW_ROUTE(app, "/freelancer/login")
|
||||
([&,configuration](const crow::request& 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";
|
||||
if (!freelancerEmail.empty() && !loginKey.empty()) {
|
||||
if (Utilities::checkFreelancerLoginState(configuration, loginKey, freelancerEmail))
|
||||
if (Utilities::checkCookieLoginState(configuration, ctx))
|
||||
templateHTML = "freelancer_Redirect_Profile.html";
|
||||
}
|
||||
|
||||
auto page = crow::mustache::load(templateHTML);
|
||||
return page.render();
|
||||
});
|
||||
|
@ -424,4 +424,20 @@ namespace Utilities {
|
||||
std::string generateExpiredCookie() {
|
||||
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