/
Shymer123
/
LumenServer
Обзор
Документация
Войти
/
Shymer123
/
LumenServer
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
Server/SourceFiles/Services/AuthService.cpp
102 строки
3 KB
Shymer123
Initial commit: LumenServer
20 июн 2026, 12:43
20 июн 2026, 12:43
173f14c
Код
Авторство
О чём код?
#include "AuthService.h" #include "Core/Utils/UuidGenerator.h" #include <iostream> namespace Services { AuthService::AuthService(Storage::Db::Repositories::AuthRepository& authRepo, TokenService& tokenService) : m_authRepo(authRepo), m_tokenService(tokenService) {} std::optional<std::string> AuthService::getAuthData(const std::string& phone) { auto user = m_authRepo.findByPhone(phone); if(!user) return std::nullopt; return user->salt; } std::optional<AuthService::AuthResult> AuthService::login(const std::string& phone, const std::string& password, const std::string& deviceId) { auto user = m_authRepo.findByPhone(phone); if(!user || user->passwordHash != password) return std::nullopt; auto tokens = m_tokenService.generateTokens(user->id, deviceId); return AuthResult { tokens, user->id }; } std::optional<AuthService::AuthResult> AuthService::registerUser(const std::string& display_name, const std::string& password, const std::string& salt, const std::string& phone, const std::string& deviceId) { if(m_authRepo.findByPhone(phone)) return std::nullopt; std::string userId = Core::Utils::generate_uuid(); Storage::Db::Repositories::UserRecord newUser{userId, "", display_name, password, salt, phone}; if(!m_authRepo.insertUser(newUser)) return std::nullopt; auto tokens = m_tokenService.generateTokens(userId, deviceId); return AuthResult { tokens, userId }; } std::optional<AuthService::AuthResult> AuthService::autoLogin(const std::string& refreshToken, const std::string& deviceId) { auto session = m_tokenService.getSessionByRefreshToken(refreshToken); if(!session) return std::nullopt; auto tokens = m_tokenService.refreshTokens(refreshToken, deviceId); if(!tokens) return std::nullopt; return AuthResult { *tokens, session->user_id }; } std::vector<Storage::Db::Repositories::SessionRecord> AuthService::getUserSessions(const std::string& userId) { return m_tokenService.getUserSessions(userId); } bool AuthService::logoutDevice(const std::string& userId, const std::string& deviceId) { return m_tokenService.revokeUserDeviceSession(userId, deviceId); } // bool AuthService::logout(const std::string& refreshToken) // { // return m_tokenService.revokeRefreshToken(refreshToken); // } bool AuthService::isUserExist(const std::string& userId) { return m_authRepo.isUserExist(userId); } std::optional<std::tuple<std::string, std::string, std::string>> AuthService::getUserData( const std::string& userId) { return m_authRepo.getUserData(userId); } std::optional<Data::Models::UserBasicInfo> AuthService::getUserByPhone(const std::string& phone) { auto user = m_authRepo.findByPhone(phone); if(!user) return std::nullopt; return Data::Models::UserBasicInfo{ user->id, user->username, user->display_name, user->phone }; } } // namespace Services