2020-01-09 12:13:39 +00:00
|
|
|
#include "accounthelper.h"
|
|
|
|
|
|
|
|
#include "../api_request.h"
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
int AccountHelper::mUserID = -1;
|
|
|
|
string AccountHelper::mToken1;
|
|
|
|
string AccountHelper::mToken2;
|
|
|
|
|
|
|
|
|
|
|
|
AccountHelper::AccountHelper()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
LoginResult AccountHelper::Login(const std::string &email, const std::string &pass)
|
|
|
|
{
|
|
|
|
auto request = ApiRequest("user/connectUSER", false);
|
|
|
|
request.addArg("userMail", email);
|
|
|
|
request.addArg("userPassword", pass);
|
|
|
|
|
|
|
|
const auto result = request.exec();
|
|
|
|
|
|
|
|
// Check for error
|
|
|
|
switch (result.code()) {
|
|
|
|
case 401:
|
|
|
|
return LoginResult::BAD_PASSWORD;
|
|
|
|
case 429:
|
|
|
|
return LoginResult::TOO_MANY_ATTEMPTS;
|
|
|
|
case 200:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return LoginResult::ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Save the tokens
|
|
|
|
const auto tokens = result.object().at("tokens").as_object();
|
|
|
|
AccountHelper::mToken1 = tokens.at("token1").as_string();
|
|
|
|
AccountHelper::mToken2 = tokens.at("token2").as_string();
|
|
|
|
|
2020-01-09 12:20:46 +00:00
|
|
|
|
|
|
|
// Get current user ID
|
|
|
|
auto response = ApiRequest("user/getCurrentUserID", true).exec();
|
|
|
|
if(response.code() != 200) return ERROR;
|
|
|
|
AccountHelper::mUserID = response.object().at("userID").as_number().to_int32();
|
|
|
|
|
2020-01-09 12:13:39 +00:00
|
|
|
return SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
int AccountHelper::userID()
|
|
|
|
{
|
|
|
|
return mUserID;
|
|
|
|
}
|
|
|
|
|
|
|
|
vector<string> AccountHelper::loginTokens()
|
|
|
|
{
|
|
|
|
auto tokens = vector<string>();
|
|
|
|
tokens.push_back(AccountHelper::mToken1);
|
|
|
|
tokens.push_back(AccountHelper::mToken2);
|
|
|
|
return tokens;
|
|
|
|
}
|