1
0
mirror of https://gitlab.com/comunic/comunicterm synced 2024-11-16 10:31:06 +00:00
comunicterm/helpers/accounthelper.cpp

89 lines
1.9 KiB
C++
Raw Permalink Normal View History

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-10 14:31:07 +00:00
if(!refreshUserID())
return ERROR;
2020-01-09 12:20:46 +00:00
2020-01-10 14:31:07 +00:00
return SUCCESS;
}
bool AccountHelper::signedIn()
{
return mToken1.length() > 0 && mToken2.length() > 0 && mUserID > 0;
}
2020-01-10 15:06:45 +00:00
void AccountHelper::SignOut()
{
mToken1 = "";
mToken2 = "";
mUserID = -1;
}
2020-01-10 14:31:07 +00:00
bool AccountHelper::refreshUserID()
{
2020-01-09 12:20:46 +00:00
// Get current user ID
auto response = ApiRequest("user/getCurrentUserID", true).exec();
2020-01-10 14:31:07 +00:00
if(response.code() != 200) return false;
2020-01-09 12:20:46 +00:00
AccountHelper::mUserID = response.object().at("userID").as_number().to_int32();
2020-01-10 14:31:07 +00:00
return true;
2020-01-09 12:13:39 +00:00
}
int AccountHelper::userID()
{
return mUserID;
}
2020-01-10 14:31:07 +00:00
void AccountHelper::setLoginTokens(std::vector<string> tokens)
{
AccountHelper::mToken1 = tokens[0];
AccountHelper::mToken2 = tokens[1];
}
2020-01-09 12:13:39 +00:00
vector<string> AccountHelper::loginTokens()
{
auto tokens = vector<string>();
tokens.push_back(AccountHelper::mToken1);
tokens.push_back(AccountHelper::mToken2);
return tokens;
}