Can perform user sign in.

This commit is contained in:
Pierre HUBERT
2018-11-28 21:33:27 +01:00
parent 9f568d5ef6
commit 99724f845c
20 changed files with 787 additions and 40 deletions

View File

@ -1,14 +1,110 @@
#include <QMessageBox>
#include "loginwidget.h"
#include "ui_loginwidget.h"
#include "../utils/accountutils.h"
#include "../helpers/accounthelper.h"
LoginWidget::LoginWidget(QWidget *parent) :
QWidget(parent),
ui(new Ui::LoginWidget)
{
ui->setupUi(this);
mAccountHelper = new AccountHelper(this);
connect(mAccountHelper, &AccountHelper::loginResult, this, &LoginWidget::loginResult);
}
LoginWidget::~LoginWidget()
{
delete ui;
}
void LoginWidget::loginResult(LoginResult result)
{
//Check if login is successfull
if(result == LoginResult::LOGIN_SUCCESS){
qDebug("User successfully signed in.");
close();
return;
}
//Release login button
ui->loginButton->setEnabled(true);
//Display appropriate error
switch (result) {
case LoginResult::INVALID_CREDENTIALS:
showError(tr("The email and / or the password was rejected by the server!"));
break;
case LoginResult::NETWORK_ERROR:
showError(tr("A network occurred. Please check your Internet connection..."));
break;
case LoginResult::TOO_MANY_REQUEST:
showError(tr("Too many login attempts from your computer. Please try again later..."));
break;
case LoginResult::OTHER_ERROR:
default:
showError(tr("Login attempt did not succeed."));
break;
}
}
void LoginWidget::on_loginButton_clicked()
{
submitForm();
}
void LoginWidget::on_emailEdit_returnPressed()
{
submitForm();
}
void LoginWidget::on_passwordEdit_returnPressed()
{
submitForm();
}
void LoginWidget::submitForm()
{
showError("");
QString emailAddress = ui->emailEdit->text();
QString password = ui->passwordEdit->text();
//Check input
if(emailAddress.length() < 5){
showError(tr("Please specify an email address!"));
return;
}
if(password.length() < 3){
showError(tr("Please specify a valid password!"));
return;
}
if(!AccountUtils::CheckEmailAddress(emailAddress)){
showError(("Please specify a valid email address!"));
return;
}
//Block login button
ui->loginButton->setEnabled(false);
//Try to sign in user
AccountLoginRequest request;
request.setEmailAddress(emailAddress);
request.setPassword(password);
mAccountHelper->login(request);
}
void LoginWidget::showError(const QString &msg)
{
ui->errorLabel->setText(msg);
}

View File

@ -3,10 +3,15 @@
#include <QWidget>
#include "../data/accountloginrequest.h"
namespace Ui {
class LoginWidget;
}
class AccountHelper;
class LoginWidget : public QWidget
{
Q_OBJECT
@ -15,8 +20,31 @@ public:
explicit LoginWidget(QWidget *parent = nullptr);
~LoginWidget();
private slots:
void loginResult(LoginResult result);
//UI Slots
void on_loginButton_clicked();
void on_emailEdit_returnPressed();
void on_passwordEdit_returnPressed();
private:
/**
* Submit login form
*/
void submitForm();
/**
* Display an error message on the widget
*
* @param msg The message to show
*/
void showError(const QString &msg);
Ui::LoginWidget *ui;
AccountHelper *mAccountHelper;
};
#endif // LOGINWIDGET_H

View File

@ -6,8 +6,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>402</width>
<height>107</height>
<width>274</width>
<height>397</height>
</rect>
</property>
<property name="windowTitle">
@ -15,47 +15,74 @@
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<layout class="QFormLayout" name="formLayout">
<item row="0" column="1">
<widget class="QLineEdit" name="emailEdit">
<property name="inputMask">
<string/>
</property>
<property name="placeholderText">
<string>Your email address</string>
</property>
</widget>
</item>
<item row="0" column="0">
<widget class="QLabel" name="label">
<property name="text">
<string>Email address</string>
</property>
</widget>
</item>
<item row="1" column="0">
<widget class="QLabel" name="label_2">
<property name="text">
<string>Password</string>
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="QLineEdit" name="passwordEdit">
<property name="placeholderText">
<string>Your password</string>
</property>
</widget>
</item>
</layout>
<spacer name="verticalSpacer_2">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>40</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QPushButton" name="pushButton">
<widget class="QLabel" name="errorLabel">
<property name="font">
<font>
<weight>75</weight>
<bold>true</bold>
</font>
</property>
<property name="text">
<string/>
</property>
<property name="alignment">
<set>Qt::AlignCenter</set>
</property>
</widget>
</item>
<item>
<widget class="QLineEdit" name="emailEdit">
<property name="inputMask">
<string/>
</property>
<property name="placeholderText">
<string>Your email address</string>
</property>
</widget>
</item>
<item>
<widget class="QLineEdit" name="passwordEdit">
<property name="echoMode">
<enum>QLineEdit::Password</enum>
</property>
<property name="placeholderText">
<string>Your password</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="loginButton">
<property name="text">
<string>Login</string>
</property>
</widget>
</item>
<item>
<spacer name="verticalSpacer">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>40</height>
</size>
</property>
</spacer>
</item>
</layout>
</widget>
<resources/>