added better login support

This commit is contained in:
2025-10-22 21:37:28 +02:00
parent 5a1297dc80
commit 89e3b95ca2
4 changed files with 25 additions and 12 deletions

View File

@@ -82,10 +82,11 @@ std::string random_string(size_t length) {
std::optional<int> loginUser(const std::string& username, const std::string& password)
{
auto sql = "SELECT id password_hash FROM users WHERE username = '?' LIMIT 1;";
auto sql = "SELECT id, password_hash FROM users WHERE username = ? LIMIT 1;";
auto db = Database();
if (!db.open())
if (!db.open()){
return {};
}
auto opt_pair = db.get<int, std::string>(sql, {username});
if (opt_pair.has_value()) {
@@ -131,7 +132,7 @@ bool initLogin(crow::SimpleApp& app)
return false;
}
// createUser("lukas", "Trollar%4928");
// createUser("lukas", "Trollar4928");
CROW_ROUTE(app, "/login").methods("GET"_method)
([](const crow::request& req){
@@ -176,13 +177,15 @@ bool initLogin(crow::SimpleApp& app)
std::optional<int> userId = loginUser(username, password);
if (!userId.has_value()) return crow::response(401, "Invalid credentials");
if (!userId.has_value()) {
return crow::response(401, "Invalid credentials");
}
// set user id
sessions[session_id].user_id = std::to_string(userId.value());
crow::response res;
res.add_header("HX-Redirect", "/dashboard"); // htmx redirect
res.add_header("HX-Redirect", "/templates/dashboard.html"); // htmx redirect
return res;
});