From 9488cd27a58860f4ba2689d921e5be4b9783f1bb Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Thu, 9 Jan 2020 13:13:39 +0100 Subject: [PATCH] Can perform basic login --- ComunicTerm.pro | 6 +++-- helpers/accounthelper.cpp | 56 +++++++++++++++++++++++++++++++++++++++ helpers/accounthelper.h | 36 +++++++++++++++++++++++++ loginscreen.cpp | 33 ++++++++++++++++++++--- 4 files changed, 126 insertions(+), 5 deletions(-) create mode 100644 helpers/accounthelper.cpp create mode 100644 helpers/accounthelper.h diff --git a/ComunicTerm.pro b/ComunicTerm.pro index 04a3ef8..e7b5bcf 100644 --- a/ComunicTerm.pro +++ b/ComunicTerm.pro @@ -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 diff --git a/helpers/accounthelper.cpp b/helpers/accounthelper.cpp new file mode 100644 index 0000000..31874e4 --- /dev/null +++ b/helpers/accounthelper.cpp @@ -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 AccountHelper::loginTokens() +{ + auto tokens = vector(); + tokens.push_back(AccountHelper::mToken1); + tokens.push_back(AccountHelper::mToken2); + return tokens; +} diff --git a/helpers/accounthelper.h b/helpers/accounthelper.h new file mode 100644 index 0000000..1b7d8a1 --- /dev/null +++ b/helpers/accounthelper.h @@ -0,0 +1,36 @@ +/** + * Account helper + * + * @author Pierre HUBERT + */ + +#pragma once + +#include +#include + +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 loginTokens(); + +private: + static int mUserID; + static std::string mToken1; + static std::string mToken2; +}; diff --git a/loginscreen.cpp b/loginscreen.cpp index aa19c38..325fc72 100644 --- a/loginscreen.cpp +++ b/loginscreen.cpp @@ -4,6 +4,8 @@ #include #include +#include + #include "loginscreen.h" #include "ui_utils.h" @@ -28,14 +30,39 @@ bool LoginScreen::exec() cbreak(); /* Line buffering disabled. pass on everything */ - string email, password; - getCredentials(email, password); + 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)