feature/move-to-svelte #1
@@ -55,6 +55,12 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function handleKeydown(event) {
|
||||||
|
if (event.ctrlKey && event.key === 's') {
|
||||||
|
saveCharacterData();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
async function saveCharacterData() {
|
async function saveCharacterData() {
|
||||||
const res = await fetch(`${API_BASE}/api/shadowrun/characters_data/${currentCharacter.id}`, {
|
const res = await fetch(`${API_BASE}/api/shadowrun/characters_data/${currentCharacter.id}`, {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
@@ -67,6 +73,8 @@
|
|||||||
let inventory = currentCharacter?.inventory || [];
|
let inventory = currentCharacter?.inventory || [];
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
|
<svelte:window on:keydown={handleKeydown} />
|
||||||
|
|
||||||
<h1>Name: {currentCharacter.name}</h1>
|
<h1>Name: {currentCharacter.name}</h1>
|
||||||
|
|
||||||
<h2>Character Info</h2>
|
<h2>Character Info</h2>
|
||||||
|
|||||||
@@ -1,8 +1,8 @@
|
|||||||
#include "login.hpp"
|
#include "login.hpp"
|
||||||
#include "crow/http_response.h"
|
#include "crow/http_response.h"
|
||||||
#include "databasepool.h"
|
#include "databasepool.h"
|
||||||
#include "utils.hpp"
|
|
||||||
#include "SessionHandler.hpp"
|
#include "SessionHandler.hpp"
|
||||||
|
#include <optional>
|
||||||
|
|
||||||
namespace login
|
namespace login
|
||||||
{
|
{
|
||||||
@@ -18,12 +18,22 @@ std::string getSessionId(const crow::request& req) {
|
|||||||
return cookie_header.substr(pos + prefix.size(), Session::SESSION_ID_SIZE);
|
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);
|
std::string sessionId = getSessionId(req);
|
||||||
if (sessionId.empty())
|
if (sessionId.empty())
|
||||||
return false;
|
return std::move(redirectToLogin());
|
||||||
|
|
||||||
auto userId = sessionHandler.isSessionValid(sessionId);
|
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)
|
std::optional<std::string> loginUser(const std::string& username, const std::string& password)
|
||||||
|
|||||||
@@ -9,29 +9,15 @@ namespace login {
|
|||||||
|
|
||||||
void initLogin(crow::SimpleApp& app);
|
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");
|
|
||||||
}
|
|
||||||
|
|
||||||
// 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);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
};
|
|
||||||
|
|
||||||
|
#define LOGGIN_REQUIERED(reg) \
|
||||||
|
{ \
|
||||||
|
auto res = login::isLoggedIn(req); \
|
||||||
|
if (res.has_value()) { \
|
||||||
|
return std::move(res.value()); \
|
||||||
|
} \
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
#endif // __LOGIN_H__
|
#endif // __LOGIN_H__
|
||||||
@@ -30,10 +30,8 @@ void initApi(crow::SimpleApp& app){
|
|||||||
|
|
||||||
CROW_ROUTE(app, "/assets/shadowrun/<path>")
|
CROW_ROUTE(app, "/assets/shadowrun/<path>")
|
||||||
([&](const crow::request& req, const std::string& p) {
|
([&](const crow::request& req, const std::string& p) {
|
||||||
if (!login::isLoggedIn(req)) {
|
LOGGIN_REQUIERED(req);
|
||||||
return crow::response(401, "Login required");
|
|
||||||
}
|
|
||||||
|
|
||||||
const filesystem::path assets_dir = "assets/shadowrun/";
|
const filesystem::path assets_dir = "assets/shadowrun/";
|
||||||
filesystem::path file_path = assets_dir / p;
|
filesystem::path file_path = assets_dir / p;
|
||||||
return utils::getFile(file_path);
|
return utils::getFile(file_path);
|
||||||
@@ -41,10 +39,7 @@ void initApi(crow::SimpleApp& app){
|
|||||||
|
|
||||||
CROW_ROUTE(app, "/api/shadowrun/characters")
|
CROW_ROUTE(app, "/api/shadowrun/characters")
|
||||||
([&](const crow::request& req) {
|
([&](const crow::request& req) {
|
||||||
|
LOGGIN_REQUIERED(req);
|
||||||
if (!login::isLoggedIn(req)) {
|
|
||||||
return crow::response(401, "Login required");
|
|
||||||
}
|
|
||||||
|
|
||||||
auto characters = getCharacters();
|
auto characters = getCharacters();
|
||||||
auto res =
|
auto res =
|
||||||
@@ -55,9 +50,7 @@ void initApi(crow::SimpleApp& app){
|
|||||||
|
|
||||||
CROW_ROUTE(app, "/api/shadowrun/characters").methods("POST"_method)
|
CROW_ROUTE(app, "/api/shadowrun/characters").methods("POST"_method)
|
||||||
([](const crow::request& req) {
|
([](const crow::request& req) {
|
||||||
if (!login::isLoggedIn(req)) {
|
LOGGIN_REQUIERED(req);
|
||||||
return crow::response(401, "Login required");
|
|
||||||
}
|
|
||||||
|
|
||||||
nlohmann::json data = nlohmann::json::parse(req.body); // parse JSON from HTTP body
|
nlohmann::json data = nlohmann::json::parse(req.body); // parse JSON from HTTP body
|
||||||
auto name = data["name"];
|
auto name = data["name"];
|
||||||
@@ -75,9 +68,8 @@ void initApi(crow::SimpleApp& app){
|
|||||||
|
|
||||||
CROW_ROUTE(app, "/api/shadowrun/characters/<int>")
|
CROW_ROUTE(app, "/api/shadowrun/characters/<int>")
|
||||||
([&](const crow::request& req, int id) {
|
([&](const crow::request& req, int id) {
|
||||||
if (!login::isLoggedIn(req)) {
|
LOGGIN_REQUIERED(req);
|
||||||
return crow::response(401, "Login required");
|
|
||||||
}
|
|
||||||
auto optCharacter = getChracter(id);
|
auto optCharacter = getChracter(id);
|
||||||
if (!optCharacter.has_value())
|
if (!optCharacter.has_value())
|
||||||
return crow::response(404, "Character not found");
|
return crow::response(404, "Character not found");
|
||||||
@@ -89,9 +81,8 @@ void initApi(crow::SimpleApp& app){
|
|||||||
|
|
||||||
CROW_ROUTE(app, "/api/shadowrun/characters_data/<int>")
|
CROW_ROUTE(app, "/api/shadowrun/characters_data/<int>")
|
||||||
([&](const crow::request& req, int id) {
|
([&](const crow::request& req, int id) {
|
||||||
if (!login::isLoggedIn(req)) {
|
LOGGIN_REQUIERED(req);
|
||||||
return crow::response(401, "Login required");
|
|
||||||
}
|
|
||||||
nlohmann::json j;
|
nlohmann::json j;
|
||||||
const auto characterData = getChracterData(id);
|
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)
|
CROW_ROUTE(app, "/api/shadowrun/characters_data/<int>").methods("POST"_method)
|
||||||
([&](const crow::request& req, int id) {
|
([&](const crow::request& req, int id) {
|
||||||
if (!login::isLoggedIn(req)) {
|
LOGGIN_REQUIERED(req);
|
||||||
return crow::response(401, "Login required");
|
|
||||||
}
|
|
||||||
nlohmann::json j = nlohmann::json::parse(req.body);
|
nlohmann::json j = nlohmann::json::parse(req.body);
|
||||||
|
|
||||||
for (auto type : magic_enum::enum_values<Type>()) {
|
for (auto type : magic_enum::enum_values<Type>()) {
|
||||||
|
|||||||
Reference in New Issue
Block a user