R-Type
Distributed multiplayer game engine in C++
Loading...
Searching...
No Matches
SessionManager.cpp
Go to the documentation of this file.
1/*
2** EPITECH PROJECT, 2025
3** Created by hugo on 06/12/2025
4** File description:
5** SessionManager.cpp
6*/
7
10
11namespace server {
12
13 SessionManager::SessionManager() : _authService(std::make_shared<AuthService>()) {}
14
15 SessionManager::SessionManager(std::shared_ptr<AuthService> authService) : _authService(authService) {}
16
17 std::string SessionManager::authenticateAndCreateSession(const std::string &username,
18 const std::string &password) {
19 if (!_authService->authenticate(username, password)) {
20 LOG_WARNING("Authentication failed for user: ", username);
21 return "";
22 }
23
24 // Generate token and create session
25 std::string token = _authService->generateToken(username);
26 std::shared_ptr<Session> session = createSession(token);
27
28 LOG_INFO("✓ User authenticated and session created: ", username);
29 return token;
30 }
31
32 std::shared_ptr<Session> SessionManager::createSession(const std::string &id) {
33 if (_sessions.find(id) != _sessions.end()) {
34 LOG_WARNING("Session ", id, " already exists");
35 return _sessions[id];
36 }
37
38 std::shared_ptr<Session> session = std::make_shared<Session>(id);
39 _sessions[id] = session;
40
41 LOG_INFO("✓ Session created: ", id);
42 return session;
43 }
44
45 std::shared_ptr<Session> SessionManager::getSession(const std::string &id) {
46 auto it = _sessions.find(id);
47 if (it != _sessions.end()) {
48 return it->second;
49 }
50 return nullptr;
51 }
52
53 void SessionManager::removeSession(const std::string &id) {
54 auto it = _sessions.find(id);
55 if (it != _sessions.end()) {
56 _sessions.erase(it);
57 LOG_INFO("✓ Session removed: ", id);
58 }
59 }
60
61} // namespace server
#define LOG_INFO(...)
Definition Logger.hpp:181
#define LOG_WARNING(...)
Definition Logger.hpp:182
std::shared_ptr< AuthService > _authService
std::shared_ptr< Session > createSession(const std::string &id) override
Create a new session.
std::string authenticateAndCreateSession(const std::string &username, const std::string &password)
Authenticate and create a session.
std::unordered_map< std::string, std::shared_ptr< Session > > _sessions
std::shared_ptr< Session > getSession(const std::string &id) override
Get session by ID.
void removeSession(const std::string &id) override
Remove session by ID.