add login and logout logic

This commit is contained in:
Sam39 2022-08-16 17:16:18 +03:00
parent a67853ea1d
commit 1d5dc1e545
4 changed files with 116 additions and 35 deletions

View File

@ -102,9 +102,11 @@ class ShutdownException : public std::runtime_error {
}; };
struct UIData { struct UIData {
static inline std::string GamePath, ProfilePath, CachePath; static inline std::string GamePath, ProfilePath, CachePath, PublicKey, UserRole, Username;
static inline bool LoginAuth{false};
}; };
void UpdateKey(const std::string& newKey);
void entry(); void entry();

View File

@ -28,7 +28,6 @@ Launcher::Launcher() :
Exit.store(false); Exit.store(false);
Launcher::StaticAbort(this); Launcher::StaticAbort(this);
DiscordTime = std::time(nullptr); DiscordTime = std::time(nullptr);
Log::Init();
WindowsInit(); WindowsInit();
SetUnhandledExceptionFilter(CrashHandler); SetUnhandledExceptionFilter(CrashHandler);
LOG(INFO) << "Starting Launcher V" << FullVersion; LOG(INFO) << "Starting Launcher V" << FullVersion;

View File

@ -23,10 +23,12 @@ std::string HTTP::Get(const std::string& IP) {
httplib::Client cli(IP.substr(0, pos)); httplib::Client cli(IP.substr(0, pos));
CliRef.store(&cli); CliRef.store(&cli);
cli.set_connection_timeout(std::chrono::seconds(5)); cli.set_connection_timeout(std::chrono::seconds(5));
cli.set_follow_location(true);
auto res = cli.Get(IP.substr(pos).c_str(), ProgressBar); auto res = cli.Get(IP.substr(pos).c_str(), ProgressBar);
std::string Ret; std::string Ret;
if (res.error() == httplib::Error::Success) { if (res.error() == httplib::Error::Success) {
LOG(INFO) << res->status;
if (res->status == 200) { if (res->status == 200) {
Ret = res->body; Ret = res->body;
} else LOG(ERROR) << res->reason; } else LOG(ERROR) << res->reason;

View File

@ -18,7 +18,7 @@
#include <comutil.h> #include <comutil.h>
#include <ShlObj.h> #include <ShlObj.h>
#include <fstream> #include <fstream>
#include <nlohmann/json.hpp> #include "Json.h"
#include "Launcher.h" #include "Launcher.h"
#include "Logger.h" #include "Logger.h"
#include "HttpAPI.h" #include "HttpAPI.h"
@ -42,11 +42,28 @@ class MyApp : public wxApp {
//static inline MyTestFrame* TestFrame; //static inline MyTestFrame* TestFrame;
}; };
/////////// AccountFrame class ///////////
class MyAccountFrame : public wxFrame {
public:
MyAccountFrame();
private:
static inline wxTextCtrl* ctrlUsername, *ctrlPassword;
bool DarkMode = wxSystemSettings::GetAppearance().IsDark();
void OnClickRegister(wxCommandEvent& event);
void OnClickLogout(wxCommandEvent& event);
void OnClickLogin(wxCommandEvent& event);
wxDECLARE_EVENT_TABLE();
};
/////////// MainFrame class /////////// /////////// MainFrame class ///////////
class MyMainFrame : public wxFrame { class MyMainFrame : public wxFrame {
public: public:
MyMainFrame(); MyMainFrame();
static void GameVersionLabel(); static void GameVersionLabel();
static inline MyAccountFrame* AccountFrame;
static inline MyMainFrame* MainFrameInstance;
void OnClickAccount(wxCommandEvent& event);
private: private:
wxStaticText* txtStatusResult; wxStaticText* txtStatusResult;
@ -56,26 +73,12 @@ class MyMainFrame : public wxFrame {
bool DarkMode = wxSystemSettings::GetAppearance().IsDark(); bool DarkMode = wxSystemSettings::GetAppearance().IsDark();
void GetStats(); void GetStats();
void OnClickAccount(wxCommandEvent& event);
void OnClickSettings(wxCommandEvent& event); void OnClickSettings(wxCommandEvent& event);
void OnClickLaunch(wxCommandEvent& event); void OnClickLaunch(wxCommandEvent& event);
void OnClickLogo(wxCommandEvent& event); void OnClickLogo(wxCommandEvent& event);
wxDECLARE_EVENT_TABLE(); wxDECLARE_EVENT_TABLE();
}; };
/////////// AccountFrame class ///////////
class MyAccountFrame : public wxFrame {
public:
MyAccountFrame();
private:
bool DarkMode = wxSystemSettings::GetAppearance().IsDark();
void OnClickRegister(wxCommandEvent& event);
void OnClickLogout(wxCommandEvent& event);
void OnClickLogin(wxCommandEvent& event);
wxDECLARE_EVENT_TABLE();
};
/////////// SettingsFrame class /////////// /////////// SettingsFrame class ///////////
class MySettingsFrame : public wxFrame { class MySettingsFrame : public wxFrame {
public: public:
@ -203,10 +206,80 @@ void LoadConfig() {
} }
} }
/////////// Login Function ///////////
bool Login(const std::string& fields) {
if (fields == "LO") {
UIData::LoginAuth = false;
UpdateKey("");
return false;
}
std::string Buffer = HTTP::Post("https://auth.beammp.com/userlogin", fields);
Json d = Json::parse(Buffer, nullptr, false);
if (Buffer == "-1") {
wxMessageBox("Failed to communicate with the auth system!", "Error");
return false;
}
if (Buffer.at(0) != '{' || d.is_discarded()) {
wxMessageBox(
"Invalid answer from authentication servers, please try again later!",
"Error");
return false;
}
if (!d["success"].is_null() && d["success"].get<bool>()) {
UIData::LoginAuth = true;
if (!d["private_key"].is_null()) {
UpdateKey(d["private_key"].get<std::string>());
}
if (!d["public_key"].is_null()) {
UIData::PublicKey = d["public_key"].get<std::string>();
}
if (!d["username"].is_null()) {
UIData::Username = d["username"].get<std::string>();
}
return true;
} else if (!d["message"].is_null()) wxMessageBox(d["message"].get<std::string>(), "Error");
return false;
}
/////////// Check Key Function ///////////
void CheckKey() {
if (fs::exists("key") && fs::file_size("key") < 100) {
std::ifstream Key("key");
if (Key.is_open()) {
auto Size = fs::file_size("key");
std::string Buffer(Size, 0);
Key.read(&Buffer[0], std::streamsize(Size));
Key.close();
Buffer = HTTP::Post("https://auth.beammp.com/userlogin",
R"({"pk":")" + Buffer + "\"}");
Json d = Json::parse(Buffer, nullptr, false);
if (Buffer == "-1" || Buffer.at(0) != '{' || d.is_discarded()) {
wxMessageBox( "Couldn't connect to auth server, you might be offline!", "Warning", wxICON_WARNING);
return;
}
if (d["success"].get<bool>()) {
UIData::LoginAuth = true;
UpdateKey(d["private_key"].get<std::string>());
UIData::PublicKey = d["public_key"].get<std::string>();
UIData::UserRole = d["role"].get<std::string>();
UIData::Username = d["username"].get<std::string>();
} else UpdateKey("");
} else UpdateKey("");
} else UpdateKey("");
}
/////////// OnInit Function /////////// /////////// OnInit Function ///////////
bool MyApp::OnInit() { bool MyApp::OnInit() {
Log::Init();
LoadConfig(); LoadConfig();
CheckKey();
auto* MainFrame = new MyMainFrame(); auto* MainFrame = new MyMainFrame();
MyMainFrame::MainFrameInstance = MainFrame;
MainFrame->SetIcon(wxIcon("icons/BeamMP_black.png",wxBITMAP_TYPE_PNG)); MainFrame->SetIcon(wxIcon("icons/BeamMP_black.png",wxBITMAP_TYPE_PNG));
// Set MainFrame properties: // Set MainFrame properties:
@ -244,10 +317,6 @@ bool MyApp::OnInit() {
return true; return true;
} }
bool isSignedIn () {
return false;
}
/////////// Windows Console Function /////////// /////////// Windows Console Function ///////////
void WindowsConsole (bool isChecked) { void WindowsConsole (bool isChecked) {
if (isChecked) { if (isChecked) {
@ -366,10 +435,10 @@ MyMainFrame::MyMainFrame() :
//Account: //Account:
auto* bitmap = new wxBitmapButton(panel, 1, wxBitmapBundle(wxImage("icons/default.png", wxBITMAP_TYPE_PNG).Scale(45,45, wxIMAGE_QUALITY_HIGH)), wxPoint(20, 560), wxSize(45,45)); auto* bitmap = new wxBitmapButton(panel, 1, wxBitmapBundle(wxImage("icons/default.png", wxBITMAP_TYPE_PNG).Scale(45,45, wxIMAGE_QUALITY_HIGH)), wxPoint(20, 560), wxSize(45,45));
if (isSignedIn()) if (UIData::LoginAuth && fs::exists( "icons/" + UIData::Username + ".png"))
bitmap->SetBitmap(wxBitmapBundle(wxImage("icons/default.png", wxBITMAP_TYPE_PNG).Scale(45,45, wxIMAGE_QUALITY_HIGH))); bitmap->SetBitmap(wxBitmapBundle(wxImage( "icons/" + UIData::Username + ".png", wxBitmapType (wxBITMAP_TYPE_PNG | wxBITMAP_TYPE_JPEG)).Scale(45, 45, wxIMAGE_QUALITY_HIGH)));
else else
bitmap->SetBitmap(wxBitmapBundle(wxImage("icons/default.png", wxBITMAP_TYPE_PNG).Scale(45,45, wxIMAGE_QUALITY_HIGH))); bitmap->SetBitmap(wxBitmapBundle(wxImage("icons/default.png", wxBITMAP_TYPE_PNG).Scale(45,45, wxIMAGE_QUALITY_HIGH)));
//Buttons: //Buttons:
auto btnSettings = new wxButton(panel, 2, wxT("Settings"), wxPoint(730,570), wxSize(110, 25)); auto btnSettings = new wxButton(panel, 2, wxT("Settings"), wxPoint(730,570), wxSize(110, 25));
@ -424,18 +493,19 @@ MyAccountFrame::MyAccountFrame() : wxFrame(nullptr, wxID_ANY, "Account Manager",
image = new wxStaticBitmap( this, wxID_ANY, wxBitmapBundle(wxImage("icons/BeamMP_black.png", wxBITMAP_TYPE_PNG).Scale(120,120, wxIMAGE_QUALITY_HIGH)), wxPoint(180,20), wxSize(120, 120)); image = new wxStaticBitmap( this, wxID_ANY, wxBitmapBundle(wxImage("icons/BeamMP_black.png", wxBITMAP_TYPE_PNG).Scale(120,120, wxIMAGE_QUALITY_HIGH)), wxPoint(180,20), wxSize(120, 120));
auto* panel = new wxPanel(this, wxID_ANY, wxPoint(), wxSize(500,650)); auto* panel = new wxPanel(this, wxID_ANY, wxPoint(), wxSize(500,650));
if (isSignedIn()) { if (UIData::LoginAuth) {
image->SetBitmap(wxBitmapBundle(wxImage("icons/default.png", wxBITMAP_TYPE_PNG).Scale(120,120, wxIMAGE_QUALITY_HIGH))); if (fs::exists( "icons/" + UIData::Username + ".png"))
image->SetBitmap(wxBitmapBundle(wxImage( "icons/" + UIData::Username + ".png", wxBITMAP_TYPE_PNG).Scale(120, 120, wxIMAGE_QUALITY_HIGH)));
else
image->SetBitmap(wxBitmapBundle(wxImage("icons/default.png", wxBITMAP_TYPE_PNG).Scale(120,120, wxIMAGE_QUALITY_HIGH)));
auto* txtName = new wxStaticText(panel, wxID_ANY, wxT("Username: BeamMP"), wxPoint(180, 200)); auto* txtName = new wxStaticText(panel, wxID_ANY, wxT("Username: " + UIData::Username), wxPoint(180, 200));
auto* txtEmail = new wxStaticText(panel, wxID_ANY, wxT("Email: beamMP@gmail.com"), wxPoint(180, 250));
auto btnLogout = new wxButton(panel, 100, wxT("Logout"), wxPoint(185,550), wxSize(110, 25)); auto btnLogout = new wxButton(panel, 100, wxT("Logout"), wxPoint(185,550), wxSize(110, 25));
//UI Colors: //UI Colors:
if (DarkMode) { if (DarkMode) {
//Text: //Text:
txtName->SetForegroundColour("white"); txtName->SetForegroundColour("white");
txtEmail->SetForegroundColour("white");
} }
} }
else { else {
@ -443,8 +513,8 @@ MyAccountFrame::MyAccountFrame() : wxFrame(nullptr, wxID_ANY, "Account Manager",
auto* txtLogin = new wxStaticText(panel, wxID_ANY, wxT("Login with your BeamMP account."), wxPoint(150, 200)); auto* txtLogin = new wxStaticText(panel, wxID_ANY, wxT("Login with your BeamMP account."), wxPoint(150, 200));
auto* ctrlUsername = new wxTextCtrl (panel, wxID_ANY, wxT(""), wxPoint(131, 230), wxSize(220,25)); ctrlUsername = new wxTextCtrl (panel, wxID_ANY, wxT(""), wxPoint(131, 230), wxSize(220,25));
auto* ctrlPassword = new wxTextCtrl (panel, wxID_ANY, wxT(""), wxPoint(131, 300), wxSize(220,25), wxTE_PASSWORD); ctrlPassword = new wxTextCtrl (panel, wxID_ANY, wxT(""), wxPoint(131, 300), wxSize(220,25), wxTE_PASSWORD);
ctrlUsername->SetHint("Username / Email"); ctrlUsername->SetHint("Username / Email");
ctrlPassword->SetHint("Password"); ctrlPassword->SetHint("Password");
@ -542,7 +612,7 @@ void MyMainFrame::GameVersionLabel() {
/////////// OnClick Account Event /////////// /////////// OnClick Account Event ///////////
void MyMainFrame::OnClickAccount(wxCommandEvent& event WXUNUSED(event)) { void MyMainFrame::OnClickAccount(wxCommandEvent& event WXUNUSED(event)) {
auto* AccountFrame = new MyAccountFrame(); AccountFrame = new MyAccountFrame();
AccountFrame->SetSize(500, 650); AccountFrame->SetSize(500, 650);
AccountFrame->Center(); AccountFrame->Center();
AccountFrame->SetIcon(wxIcon("icons/BeamMP_black.png",wxBITMAP_TYPE_PNG)); AccountFrame->SetIcon(wxIcon("icons/BeamMP_black.png",wxBITMAP_TYPE_PNG));
@ -611,15 +681,23 @@ void MyAccountFrame::OnClickRegister(wxCommandEvent& event WXUNUSED(event)) {
/////////// OnClick Login Event /////////// /////////// OnClick Login Event ///////////
void MyAccountFrame::OnClickLogin(wxCommandEvent& event WXUNUSED(event)) { void MyAccountFrame::OnClickLogin(wxCommandEvent& event WXUNUSED(event)) {
Json json;
json ["password"] = ctrlPassword->GetValue().utf8_string();
json ["username"] = ctrlUsername->GetValue().utf8_string();
if (Login(json.dump())) {
HTTP::Download("https://forum.beammp.com/user_avatar/forum.beammp.com/" + UIData::Username + "/240/4411_2.png", "icons/" + UIData::Username + ".png");
MyMainFrame::AccountFrame->Destroy();
MyMainFrame::MainFrameInstance->OnClickAccount(event);
}
} }
/////////// OnClick Logout Event /////////// /////////// OnClick Logout Event ///////////
void MyAccountFrame::OnClickLogout(wxCommandEvent& event WXUNUSED(event)) { void MyAccountFrame::OnClickLogout(wxCommandEvent& event WXUNUSED(event)) {
Login("LO");
MyMainFrame::AccountFrame->Destroy();
MyMainFrame::MainFrameInstance->OnClickAccount(event);
} }
/////////// OnClick Console Event /////////// /////////// OnClick Console Event ///////////