added better login support
This commit is contained in:
@@ -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;
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user