diff --git a/core/http/session_manager.cpp b/core/http/session_manager.cpp index bf56d7d..df91e0a 100644 --- a/core/http/session_manager.cpp +++ b/core/http/session_manager.cpp @@ -8,6 +8,8 @@ #include "request.h" +#include "cookie.h" + void SessionManager::add_session(HTTPSession *session) { if (!session) { printf("SessionManager::add_session: ERROR, session is null!\n"); @@ -72,8 +74,6 @@ HTTPSession *SessionManager::create_session() { _sessions_vec.push_back(session); _sessions[session->session_id] = session; - add_session(session); - lock.unlock(); return session; @@ -114,13 +114,23 @@ std::string SessionManager::generate_session_id(const std::string &base) { } void SessionManager::session_setup_middleware(Object *instance, Request *request) { - const std::string &sid = request->get_cookie("session"); + const std::string &sid = request->get_cookie("session_id"); if (sid == "") { + //You could create a session here if you want to always have sessions + //Example code: + //HTTPSession *session = SessionManager::get_singleton()->create_session(); + //request->session = session; + //request->add_cookie(::Cookie("session_id", session->session_id)); + + request->next_stage(); + return; } request->session = SessionManager::get_singleton()->get_session(sid); + + request->next_stage(); } SessionManager *SessionManager::get_singleton() { diff --git a/core/http/web_application.cpp b/core/http/web_application.cpp index 471e76f..a095df0 100644 --- a/core/http/web_application.cpp +++ b/core/http/web_application.cpp @@ -22,6 +22,9 @@ void WebApplication::setup_routes() { } void WebApplication::setup_middleware() { + //If you want sessions add this to your inherited class. Should probably be the first one. + //middlewares.push_back(HandlerInstance(::SessionManager::session_setup_middleware)); + middlewares.push_back(HandlerInstance([this](Object *instance, Request *request){ this->default_routing_middleware(instance, request); })); }