1
0
mirror of https://gitlab.com/comunic/comunicterm synced 2024-12-26 05:28:54 +00:00

Can perform basic login

This commit is contained in:
Pierre HUBERT 2020-01-09 13:13:39 +01:00
parent 2994919cc6
commit 9488cd27a5
4 changed files with 126 additions and 5 deletions

View File

@ -10,11 +10,13 @@ SOURCES += \
api_request.cpp \
apiresponse.cpp \
loginscreen.cpp \
ui_utils.cpp
ui_utils.cpp \
helpers/accounthelper.cpp
HEADERS += \
config.h \
api_request.h \
apiresponse.h \
loginscreen.h \
ui_utils.h
ui_utils.h \
helpers/accounthelper.h

56
helpers/accounthelper.cpp Normal file
View 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
View 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;
};

View File

@ -4,6 +4,8 @@
#include <string>
#include <iostream>
#include <helpers/accounthelper.h>
#include "loginscreen.h"
#include "ui_utils.h"
@ -28,14 +30,39 @@ bool LoginScreen::exec()
cbreak(); /* Line buffering disabled. pass on everything */
LoginResult res;
do {
string email, password;
getCredentials(email, password);
mvprintw(3,0, "Done!");
res = AccountHelper::Login(email, password);
clear();
switch(res) {
case ERROR:
ui_utils::alert(stdscr, "An error occured while trying to sign in!");
break;
case TOO_MANY_ATTEMPTS:
ui_utils::alert(stdscr, "Too many attempt! Please try again later...");
break;
case BAD_PASSWORD:
ui_utils::alert(stdscr, "Email / password invalid!");
break;
case SUCCESS:
break;
}
} while(res != SUCCESS);
getch(); /* Wait for user input */
endwin(); /* End curses mode */
return true;
}
void LoginScreen::getCredentials(string &email, string &pass)