refactor: improve auth dialog implementation

This commit is contained in:
talksik
2026-01-27 11:55:51 -08:00
parent 88b10a4380
commit 707e4455ac
3 changed files with 212 additions and 206 deletions
+71 -100
View File
@@ -1,118 +1,89 @@
#include "authdialog.h"
#include "ui_authdialog.h"
#include "authmanager.h"
AuthDialog::AuthDialog(AuthManager *authManager, QWidget *parent)
: QDialog{parent}, m_authManager(authManager), m_state(ENTER_EMAIL)
: QDialog{parent}, m_authManager(authManager), ui(new Ui::Dialog)
{
ui = new Ui::Dialog;
ui->setupUi(this);
// initialize form based on current state
updateForm();
// Connect button signals
connect(ui->sendCodeButton, &QPushButton::clicked, this, &AuthDialog::onSendCodeClicked);
connect(ui->signInButton, &QPushButton::clicked, this, &AuthDialog::onSignInClicked);
connect(ui->goBackButton, &QPushButton::clicked, this, [this]() { showEmailPage(); });
connect(ui->sendCodeButton, &QPushButton::clicked, this, [this]() {
QString emailInput = ui->emailLineEdit->text();
if (emailInput.isEmpty())
{
ui->errorMessage->setText("Must provide a valid email");
return;
}
ui->errorMessage->setText("");
m_authManager->requestSignInCode(emailInput);
// Connect AuthManager signals
connect(m_authManager, &AuthManager::codeSentToEmail, this, [this](const QString &) {
showCodePage();
});
connect(ui->signInButton, &QPushButton::clicked, this, [this]() {
QString codeInput = ui->codeLineEdit->text();
if (codeInput.isEmpty())
{
ui->errorMessage->setText("Must provide a valid code");
return;
}
ui->errorMessage->setText("");
m_authManager->signIn(codeInput);
});
connect(m_authManager, &AuthManager::codeSentToEmail, this, [this](const QString &email) {
m_state = ENTER_CODE;
updateForm();
});
connect(m_authManager, &AuthManager::isSignedInChanged, this, [this]() {
if (m_authManager->isSignedIn())
{
m_state = SIGNED_IN;
}
else
{
m_state = ENTER_EMAIL;
}
updateForm();
});
connect(m_authManager, &AuthManager::errorOccurred, this, [this](const QString &message) {
ui->errorMessage->setText(message);
ui->errorMessage->show();
setError(message);
});
}
void AuthDialog::reset()
{
m_state = ENTER_EMAIL;
ui->emailLineEdit->clear();
ui->codeLineEdit->clear();
updateForm();
}
void AuthDialog::updateForm()
{
ui->errorMessage->setText("");
switch (m_state)
{
case ENTER_EMAIL:
ui->successLabel->hide();
ui->sendCodeButton->show();
ui->emailLineEdit->show();
ui->emailLabel->show();
ui->signInButton->hide();
ui->codeLabel->hide();
ui->codeLineEdit->hide();
break;
case ENTER_CODE:
ui->successLabel->hide();
ui->sendCodeButton->hide();
ui->emailLineEdit->hide();
ui->emailLabel->hide();
ui->signInButton->show();
ui->codeLabel->show();
ui->codeLineEdit->show();
break;
case SIGNED_IN:
ui->successLabel->show();
ui->sendCodeButton->hide();
ui->emailLineEdit->hide();
ui->emailLabel->hide();
ui->signInButton->hide();
ui->codeLabel->hide();
ui->codeLineEdit->hide();
break;
default:
break;
}
// Start on email page
showEmailPage();
}
AuthDialog::~AuthDialog()
{
delete ui;
}
void AuthDialog::reset()
{
ui->emailLineEdit->clear();
ui->codeLineEdit->clear();
showEmailPage();
}
void AuthDialog::showEmailPage()
{
ui->stackedWidget->setCurrentIndex(0);
ui->emailLineEdit->setFocus();
setError("");
}
void AuthDialog::showCodePage()
{
ui->stackedWidget->setCurrentIndex(1);
ui->codeLineEdit->setFocus();
setError("");
}
void AuthDialog::setError(const QString &message)
{
// Update error labels on both pages
ui->errorMessage->setText(message);
ui->codeErrorMessage->setText(message);
// Show/hide based on whether there's an error
bool hasError = !message.isEmpty();
ui->errorMessage->setVisible(hasError);
ui->codeErrorMessage->setVisible(hasError);
}
void AuthDialog::onSendCodeClicked()
{
QString email = ui->emailLineEdit->text().trimmed();
if (email.isEmpty())
{
setError("Please enter your email");
return;
}
setError("");
m_authManager->requestSignInCode(email);
}
void AuthDialog::onSignInClicked()
{
QString code = ui->codeLineEdit->text().trimmed();
if (code.isEmpty())
{
setError("Please enter the code");
return;
}
setError("");
m_authManager->signIn(code);
}
+12 -15
View File
@@ -1,37 +1,34 @@
#ifndef AUTHDIALOG_H
#define AUTHDIALOG_H
#include <QObject>
#include <QWidget>
#include "ui_authdialog.h"
#include <QDialog>
class AuthManager;
namespace Ui {
class Dialog;
}
class AuthDialog : public QDialog
{
Q_OBJECT
public:
explicit AuthDialog(AuthManager *, QWidget *parent = nullptr);
explicit AuthDialog(AuthManager *authManager, QWidget *parent = nullptr);
~AuthDialog();
enum State
{
ENTER_EMAIL,
ENTER_CODE,
SIGNED_IN
};
void reset();
signals:
private slots:
void onSendCodeClicked();
void onSignInClicked();
private:
Ui::Dialog *ui;
AuthManager *m_authManager;
State m_state;
void updateForm();
void showEmailPage();
void showCodePage();
void setError(const QString &message);
};
#endif // AUTHDIALOG_H
+129 -91
View File
@@ -13,102 +13,140 @@
<property name="windowTitle">
<string>Dialog</string>
</property>
<widget class="QGroupBox" name="groupBox">
<widget class="QStackedWidget" name="stackedWidget">
<property name="geometry">
<rect>
<x>80</x>
<y>30</y>
<width>231</width>
<height>183</height>
<x>40</x>
<y>20</y>
<width>321</width>
<height>241</height>
</rect>
</property>
<property name="title">
<string>Sign in</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QLabel" name="emailLabel">
<property name="text">
<string>Email</string>
</property>
</widget>
</item>
<item>
<widget class="QLineEdit" name="emailLineEdit">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="cursor">
<cursorShape>IBeamCursor</cursorShape>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="codeLabel">
<property name="text">
<string>Code</string>
</property>
</widget>
</item>
<item>
<widget class="QLineEdit" name="codeLineEdit"/>
</item>
<item>
<widget class="QLabel" name="errorMessage">
<property name="text">
<string>Error</string>
</property>
</widget>
</item>
</layout>
</widget>
<widget class="QPushButton" name="sendCodeButton">
<property name="geometry">
<rect>
<x>230</x>
<y>250</y>
<width>101</width>
<height>32</height>
</rect>
</property>
<property name="text">
<string>Send code</string>
</property>
<property name="default">
<bool>true</bool>
</property>
</widget>
<widget class="QPushButton" name="signInButton">
<property name="geometry">
<rect>
<x>230</x>
<y>250</y>
<width>101</width>
<height>32</height>
</rect>
</property>
<property name="text">
<string>Sign in</string>
</property>
<property name="default">
<bool>true</bool>
</property>
</widget>
<widget class="QLabel" name="successLabel">
<property name="geometry">
<rect>
<x>120</x>
<y>120</y>
<width>121</width>
<height>16</height>
</rect>
</property>
<property name="text">
<string>Successful</string>
<property name="currentIndex">
<number>0</number>
</property>
<widget class="QWidget" name="enterEmailPage">
<widget class="QGroupBox" name="groupBox">
<property name="geometry">
<rect>
<x>50</x>
<y>20</y>
<width>231</width>
<height>183</height>
</rect>
</property>
<property name="title">
<string>Sign in</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QLabel" name="emailLabel">
<property name="text">
<string>Email</string>
</property>
</widget>
</item>
<item>
<widget class="QLineEdit" name="emailLineEdit">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="cursor">
<cursorShape>IBeamCursor</cursorShape>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="errorMessage">
<property name="text">
<string>Error</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="sendCodeButton">
<property name="text">
<string>Send code</string>
</property>
<property name="default">
<bool>true</bool>
</property>
</widget>
</item>
</layout>
</widget>
</widget>
<widget class="QWidget" name="enterCodePage">
<widget class="QGroupBox" name="groupBox_2">
<property name="geometry">
<rect>
<x>50</x>
<y>20</y>
<width>231</width>
<height>201</height>
</rect>
</property>
<property name="title">
<string>Verify code</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout_2">
<item>
<widget class="QLabel" name="codeLabel">
<property name="text">
<string>Enter the code we sent to your email</string>
</property>
<property name="wordWrap">
<bool>true</bool>
</property>
</widget>
</item>
<item>
<widget class="QLineEdit" name="codeLineEdit">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="cursor">
<cursorShape>IBeamCursor</cursorShape>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="codeErrorMessage">
<property name="text">
<string>Error</string>
</property>
</widget>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QPushButton" name="goBackButton">
<property name="text">
<string>Cancel</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="signInButton">
<property name="text">
<string>Sign In</string>
</property>
<property name="default">
<bool>true</bool>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</widget>
</widget>
</widget>
</widget>
<resources/>