mirror of
https://gitlab.com/comunic/comunicterm
synced 2025-06-19 16:45:17 +00:00
Can perform basic login
This commit is contained in:
56
helpers/accounthelper.cpp
Normal file
56
helpers/accounthelper.cpp
Normal file
@ -0,0 +1,56 @@
|
||||
#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();
|
||||
|
||||
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;
|
||||
}
|
36
helpers/accounthelper.h
Normal file
36
helpers/accounthelper.h
Normal file
@ -0,0 +1,36 @@
|
||||
/**
|
||||
* Account helper
|
||||
*
|
||||
* @author Pierre HUBERT
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
enum LoginResult {
|
||||
ERROR,
|
||||
BAD_PASSWORD,
|
||||
TOO_MANY_ATTEMPTS,
|
||||
SUCCESS
|
||||
};
|
||||
|
||||
class AccountHelper
|
||||
{
|
||||
public:
|
||||
AccountHelper();
|
||||
|
||||
static LoginResult Login(const std::string &email,
|
||||
const std::string &pass);
|
||||
|
||||
|
||||
static int userID();
|
||||
|
||||
static std::vector<std::string> loginTokens();
|
||||
|
||||
private:
|
||||
static int mUserID;
|
||||
static std::string mToken1;
|
||||
static std::string mToken2;
|
||||
};
|
Reference in New Issue
Block a user