feature/move-to-svelte #1

Merged
lukas merged 19 commits from feature/move-to-svelte into main 2026-02-14 22:48:51 +01:00
4 changed files with 39 additions and 46 deletions
Showing only changes of commit 4ad48d4ecc - Show all commits

View File

@@ -55,6 +55,12 @@
}
}
function handleKeydown(event) {
if (event.ctrlKey && event.key === 's') {
saveCharacterData();
}
}
async function saveCharacterData() {
const res = await fetch(`${API_BASE}/api/shadowrun/characters_data/${currentCharacter.id}`, {
method: 'POST',
@@ -67,6 +73,8 @@
let inventory = currentCharacter?.inventory || [];
</script>
<svelte:window on:keydown={handleKeydown} />
<h1>Name: {currentCharacter.name}</h1>
<h2>Character Info</h2>

View File

@@ -1,8 +1,8 @@
#include "login.hpp"
#include "crow/http_response.h"
#include "databasepool.h"
#include "utils.hpp"
#include "SessionHandler.hpp"
#include <optional>
namespace login
{
@@ -18,12 +18,22 @@ std::string getSessionId(const crow::request& req) {
return cookie_header.substr(pos + prefix.size(), Session::SESSION_ID_SIZE);
}
bool isLoggedIn(const crow::request& req) {
static crow::response redirectToLogin(){
crow::response res(302); // 302 = temporary redirect
res.set_header("Location", "/");
return res;
}
std::optional<crow::response> isLoggedIn(const crow::request& req) {
std::string sessionId = getSessionId(req);
if (sessionId.empty())
return false;
return std::move(redirectToLogin());
auto userId = sessionHandler.isSessionValid(sessionId);
return userId.has_value();
if(!userId.has_value())
return std::move(redirectToLogin());
return {};
}
std::optional<std::string> loginUser(const std::string& username, const std::string& password)

View File

@@ -9,29 +9,15 @@ namespace login {
void initLogin(crow::SimpleApp& app);
bool isLoggedIn(const crow::request& req);
std::optional<crow::response> isLoggedIn(const crow::request& req);
// login_required lambda that works for any handler with arbitrary args
inline auto login_required = [](auto handler){
return [handler](auto&&... args) -> crow::response {
// the first argument is always crow::request
const crow::request& req = std::get<0>(std::forward_as_tuple(args...));
if (!isLoggedIn(req)) {
return crow::response(401, "Login required");
#define LOGGIN_REQUIERED(reg) \
{ \
auto res = login::isLoggedIn(req); \
if (res.has_value()) { \
return std::move(res.value()); \
} \
}
// call original handler with all arguments
auto result = handler(std::forward<decltype(args)>(args)...);
// ensure crow::response return type
if constexpr (std::is_same_v<decltype(result), crow::response>) {
return result;
} else {
return crow::response(result);
}
};
};
}
#endif // __LOGIN_H__

View File

@@ -30,9 +30,7 @@ void initApi(crow::SimpleApp& app){
CROW_ROUTE(app, "/assets/shadowrun/<path>")
([&](const crow::request& req, const std::string& p) {
if (!login::isLoggedIn(req)) {
return crow::response(401, "Login required");
}
LOGGIN_REQUIERED(req);
const filesystem::path assets_dir = "assets/shadowrun/";
filesystem::path file_path = assets_dir / p;
@@ -41,10 +39,7 @@ void initApi(crow::SimpleApp& app){
CROW_ROUTE(app, "/api/shadowrun/characters")
([&](const crow::request& req) {
if (!login::isLoggedIn(req)) {
return crow::response(401, "Login required");
}
LOGGIN_REQUIERED(req);
auto characters = getCharacters();
auto res =
@@ -55,9 +50,7 @@ void initApi(crow::SimpleApp& app){
CROW_ROUTE(app, "/api/shadowrun/characters").methods("POST"_method)
([](const crow::request& req) {
if (!login::isLoggedIn(req)) {
return crow::response(401, "Login required");
}
LOGGIN_REQUIERED(req);
nlohmann::json data = nlohmann::json::parse(req.body); // parse JSON from HTTP body
auto name = data["name"];
@@ -75,9 +68,8 @@ void initApi(crow::SimpleApp& app){
CROW_ROUTE(app, "/api/shadowrun/characters/<int>")
([&](const crow::request& req, int id) {
if (!login::isLoggedIn(req)) {
return crow::response(401, "Login required");
}
LOGGIN_REQUIERED(req);
auto optCharacter = getChracter(id);
if (!optCharacter.has_value())
return crow::response(404, "Character not found");
@@ -89,9 +81,8 @@ void initApi(crow::SimpleApp& app){
CROW_ROUTE(app, "/api/shadowrun/characters_data/<int>")
([&](const crow::request& req, int id) {
if (!login::isLoggedIn(req)) {
return crow::response(401, "Login required");
}
LOGGIN_REQUIERED(req);
nlohmann::json j;
const auto characterData = getChracterData(id);
@@ -119,9 +110,7 @@ void initApi(crow::SimpleApp& app){
CROW_ROUTE(app, "/api/shadowrun/characters_data/<int>").methods("POST"_method)
([&](const crow::request& req, int id) {
if (!login::isLoggedIn(req)) {
return crow::response(401, "Login required");
}
LOGGIN_REQUIERED(req);
nlohmann::json j = nlohmann::json::parse(req.body);
for (auto type : magic_enum::enum_values<Type>()) {