#include <Authenticator/SessionAuthenticator/SessionAuthenticator.hpp>
#include <Protocol/Protocol.hpp>

SessionAuthenticator::SessionAuthenticator(std::string userName, UserDB::SessionID sid) :
    userName(userName),
    sid(sid)
{};

bool SessionAuthenticator::sendGreetingPayload(SOCKET socket)
{
    // Send a LoginHeader.
    Protocol::LoginHeader loginHeader;
    loginHeader.authType = Protocol::AuthenticationType::SessionID;
    loginHeader.payloadSize = userName.size() + sizeof(sid);
    if (!writeSocket(socket, (char*) &loginHeader, sizeof(loginHeader)))
    {
        return false;
    };

    // Send the session ID.
    if (!writeSocket(socket, (char*) &sid, sizeof(sid)))
    {
        return false;
    };

    // Send the user name.
    if (!writeSocket(socket, userName.c_str(), userName.size()))
    {
        return false;
    };

    return true;
};

std::string SessionAuthenticator::getUserName()
{
    return userName;
};