added better login support
This commit is contained in:
parent
5a1297dc80
commit
89e3b95ca2
@ -1,4 +1,3 @@
|
|||||||
#include "crow.h"
|
|
||||||
#include "utils.hpp"
|
#include "utils.hpp"
|
||||||
#include "database.hpp"
|
#include "database.hpp"
|
||||||
#include <optional>
|
#include <optional>
|
||||||
|
|||||||
@ -8,6 +8,7 @@
|
|||||||
#include <optional>
|
#include <optional>
|
||||||
#include <set>
|
#include <set>
|
||||||
#include <map>
|
#include <map>
|
||||||
|
#include "crow.h"
|
||||||
|
|
||||||
class Database {
|
class Database {
|
||||||
|
|
||||||
@ -28,7 +29,7 @@ public:
|
|||||||
template <typename T>
|
template <typename T>
|
||||||
std::optional<T> getSqlData(sqlite3_stmt* stmt, int i)
|
std::optional<T> getSqlData(sqlite3_stmt* stmt, int i)
|
||||||
{
|
{
|
||||||
if ((stmt == nullptr) || (sqlite3_step(stmt) != SQLITE_ROW))
|
if (stmt == nullptr)
|
||||||
return {};
|
return {};
|
||||||
|
|
||||||
if constexpr (std::is_same_v<T, int>) {
|
if constexpr (std::is_same_v<T, int>) {
|
||||||
@ -51,9 +52,10 @@ public:
|
|||||||
template <typename T1, typename T2>
|
template <typename T1, typename T2>
|
||||||
std::optional<std::pair<T1, T2>> get(const std::string sql, const QueryData& data){
|
std::optional<std::pair<T1, T2>> get(const std::string sql, const QueryData& data){
|
||||||
sqlite3_stmt* stmt = bind(sql, data);
|
sqlite3_stmt* stmt = bind(sql, data);
|
||||||
if ((stmt == nullptr) || (sqlite3_step(stmt) != SQLITE_ROW))
|
if ( (stmt == nullptr) || (sqlite3_step(stmt) != SQLITE_ROW ) ) {
|
||||||
|
CROW_LOG_ERROR << "Failed to run statement: " << sqlite3_errmsg(m_db);
|
||||||
return {};
|
return {};
|
||||||
|
}
|
||||||
std::optional<T1> v1 = getSqlData<T1>(stmt, 0);
|
std::optional<T1> v1 = getSqlData<T1>(stmt, 0);
|
||||||
std::optional<T2> v2 = getSqlData<T2>(stmt, 1);
|
std::optional<T2> v2 = getSqlData<T2>(stmt, 1);
|
||||||
|
|
||||||
|
|||||||
@ -82,10 +82,11 @@ std::string random_string(size_t length) {
|
|||||||
|
|
||||||
std::optional<int> loginUser(const std::string& username, const std::string& password)
|
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();
|
auto db = Database();
|
||||||
if (!db.open())
|
if (!db.open()){
|
||||||
return {};
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
auto opt_pair = db.get<int, std::string>(sql, {username});
|
auto opt_pair = db.get<int, std::string>(sql, {username});
|
||||||
if (opt_pair.has_value()) {
|
if (opt_pair.has_value()) {
|
||||||
@ -131,7 +132,7 @@ bool initLogin(crow::SimpleApp& app)
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// createUser("lukas", "Trollar%4928");
|
// createUser("lukas", "Trollar4928");
|
||||||
|
|
||||||
CROW_ROUTE(app, "/login").methods("GET"_method)
|
CROW_ROUTE(app, "/login").methods("GET"_method)
|
||||||
([](const crow::request& req){
|
([](const crow::request& req){
|
||||||
@ -176,13 +177,15 @@ bool initLogin(crow::SimpleApp& app)
|
|||||||
|
|
||||||
std::optional<int> userId = loginUser(username, password);
|
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
|
// set user id
|
||||||
sessions[session_id].user_id = std::to_string(userId.value());
|
sessions[session_id].user_id = std::to_string(userId.value());
|
||||||
|
|
||||||
crow::response res;
|
crow::response res;
|
||||||
res.add_header("HX-Redirect", "/dashboard"); // htmx redirect
|
res.add_header("HX-Redirect", "/templates/dashboard.html"); // htmx redirect
|
||||||
return res;
|
return res;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
15
src/main.cpp
15
src/main.cpp
@ -16,15 +16,24 @@ int main() {
|
|||||||
crow::SimpleApp app;
|
crow::SimpleApp app;
|
||||||
|
|
||||||
CROW_ROUTE(app, "/")([] {
|
CROW_ROUTE(app, "/")([] {
|
||||||
return crow::response(utils::loadFile("templates/index.html"));
|
auto data = utils::loadFile("templates/index.html");
|
||||||
|
if (data.empty())
|
||||||
|
return crow::response(404);
|
||||||
|
return crow::response(data);
|
||||||
});
|
});
|
||||||
|
|
||||||
CROW_ROUTE(app, "/static/<string>")([](const std::string& file) {
|
CROW_ROUTE(app, "/static/<string>")([](const std::string& file) {
|
||||||
return crow::response(utils::loadFile("static/" + file));
|
auto data = utils::loadFile("static/" + file);
|
||||||
|
if (data.empty())
|
||||||
|
return crow::response(404);
|
||||||
|
return crow::response(data);
|
||||||
});
|
});
|
||||||
|
|
||||||
CROW_ROUTE(app, "/templates/<string>")([](const std::string& file) {
|
CROW_ROUTE(app, "/templates/<string>")([](const std::string& file) {
|
||||||
return crow::response(utils::loadFile("templates/" + file));
|
auto data = utils::loadFile("templates/" + file);
|
||||||
|
if (data.empty())
|
||||||
|
return crow::response(404);
|
||||||
|
return crow::response(data);
|
||||||
});
|
});
|
||||||
|
|
||||||
// Static file redirector
|
// Static file redirector
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user