mirror of
https://gitlab.com/comunic/comunicwatcher
synced 2024-11-21 12:59:26 +00:00
Improve code structure
This commit is contained in:
parent
dde3ad35df
commit
b9b509aca1
@ -14,12 +14,14 @@ DEFINES += QT_DEPRECATED_WARNINGS
|
||||
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
|
||||
|
||||
SOURCES += \
|
||||
accounthelper.cpp \
|
||||
apirequest.cpp \
|
||||
apiresponse.cpp \
|
||||
main.cpp \
|
||||
loginwindow.cpp
|
||||
|
||||
HEADERS += \
|
||||
accounthelper.h \
|
||||
apirequest.h \
|
||||
apiresponse.h \
|
||||
config.h \
|
||||
|
42
accounthelper.cpp
Normal file
42
accounthelper.cpp
Normal file
@ -0,0 +1,42 @@
|
||||
#include "accounthelper.h"
|
||||
#include "apirequest.h"
|
||||
|
||||
AccountHelper::AccountHelper()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void AccountHelper::LoginUser(const QString &email, const QString &password, const vLoginCallback cb)
|
||||
{
|
||||
auto req = new APIRequest("account/login");
|
||||
req->addString("userMail", email);
|
||||
req->addString("userPassword", password);
|
||||
req->exec();
|
||||
QObject::connect(req, &APIRequest::done, [=] (APIResponse res) {
|
||||
|
||||
QString token;
|
||||
|
||||
switch(res.getCode()) {
|
||||
|
||||
case 200:
|
||||
// Save login token
|
||||
token = res.getObject().value("tokens").toObject().value("token1").toString();
|
||||
|
||||
cb(LoginResult::SUCCESS);
|
||||
break;
|
||||
|
||||
case 401:
|
||||
cb(LoginResult::BAD_PASSWORD);
|
||||
break;
|
||||
|
||||
case 429:
|
||||
cb(LoginResult::TOO_MANY_REQUEST);
|
||||
break;
|
||||
|
||||
default:
|
||||
cb(LoginResult::ERROR);
|
||||
break;
|
||||
}
|
||||
|
||||
});
|
||||
}
|
35
accounthelper.h
Normal file
35
accounthelper.h
Normal file
@ -0,0 +1,35 @@
|
||||
/**
|
||||
* Login helper
|
||||
*
|
||||
* @author Pierre Hubert
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <functional>
|
||||
|
||||
#include <QObject>
|
||||
|
||||
enum LoginResult {
|
||||
SUCCESS,
|
||||
BAD_PASSWORD,
|
||||
TOO_MANY_REQUEST,
|
||||
ERROR
|
||||
};
|
||||
|
||||
typedef std::function<void (LoginResult)> vLoginCallback;
|
||||
|
||||
class AccountHelper
|
||||
{
|
||||
public:
|
||||
AccountHelper();
|
||||
|
||||
/**
|
||||
* Attempt to login user
|
||||
*
|
||||
* @param email User email address
|
||||
* @param password User password
|
||||
*/
|
||||
static void LoginUser(const QString &email, const QString &password, const vLoginCallback cb);
|
||||
};
|
||||
|
@ -1,3 +1,4 @@
|
||||
#include "accounthelper.h"
|
||||
#include "apirequest.h"
|
||||
#include "loginwindow.h"
|
||||
#include "ui_loginwindow.h"
|
||||
@ -51,40 +52,39 @@ void LoginWindow::submitForm()
|
||||
|
||||
setLoading(true);
|
||||
|
||||
auto req = new APIRequest("account/login");
|
||||
req->addString("userMail", ui->emailEdit->text());
|
||||
req->addString("userPassword", ui->passwordEdit->text());
|
||||
req->exec();
|
||||
connect(req, &APIRequest::done, this, &LoginWindow::onResponse);
|
||||
AccountHelper::LoginUser(
|
||||
ui->emailEdit->text(),
|
||||
ui->passwordEdit->text(),
|
||||
[&](LoginResult res) {
|
||||
this->onResponse(res);
|
||||
});
|
||||
}
|
||||
|
||||
void LoginWindow::onResponse(APIResponse res)
|
||||
void LoginWindow::onResponse(LoginResult res)
|
||||
{
|
||||
if(res.isError()) {
|
||||
QString msg;
|
||||
QString msg;
|
||||
|
||||
switch(res.getCode()) {
|
||||
case 401:
|
||||
msg = tr("Invalid credentials!");
|
||||
break;
|
||||
|
||||
case 429:
|
||||
msg = tr("Too many login attempt, please try again later!");
|
||||
break;
|
||||
|
||||
default:
|
||||
msg = tr("An error occured while trying to sign you in!");
|
||||
break;
|
||||
}
|
||||
|
||||
setLoading(false);
|
||||
QMessageBox::warning(this, tr("Login failed"), msg);
|
||||
switch(res) {
|
||||
|
||||
case SUCCESS:
|
||||
QMessageBox::information(this, "ok", "success");
|
||||
return;
|
||||
|
||||
case BAD_PASSWORD:
|
||||
msg = tr("Invalid credentials!");
|
||||
break;
|
||||
|
||||
case TOO_MANY_REQUEST:
|
||||
msg = tr("Too many login attempt, please try again later!");
|
||||
break;
|
||||
|
||||
default:
|
||||
msg = tr("An error occured while trying to sign you in!");
|
||||
break;
|
||||
}
|
||||
|
||||
auto token = res.getObject().value("tokens").toObject().value("token1").toString();
|
||||
QMessageBox::information(this, "ok", token);
|
||||
setLoading(false);
|
||||
QMessageBox::warning(this, tr("Login failed"), msg);
|
||||
}
|
||||
|
||||
void LoginWindow::on_submitButton_clicked()
|
||||
|
@ -7,6 +7,7 @@
|
||||
#pragma once
|
||||
|
||||
#include "apiresponse.h"
|
||||
#include "accounthelper.h"
|
||||
|
||||
#include <QDialog>
|
||||
#include <QMouseEvent>
|
||||
@ -33,7 +34,7 @@ private slots:
|
||||
*/
|
||||
void submitForm();
|
||||
|
||||
void onResponse(APIResponse res);
|
||||
void onResponse(LoginResult res);
|
||||
|
||||
void on_closeButton_clicked();
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user