refactor: use proper qt widgets patterns for auth

This commit is contained in:
talksik
2026-02-03 19:47:58 -08:00
parent 6591dfa2f6
commit dd54ce6a25
6 changed files with 74 additions and 57 deletions
+10 -23
View File
@@ -22,7 +22,7 @@ MainWindow::MainWindow(QWidget *parent)
m_authDialog(nullptr), m_data(nullptr), m_networkStreamModel(nullptr), m_particleListModel(nullptr),
m_selectedParticleRow(-1), m_isDragging(false)
{
m_authDialog = new AuthDialog(m_authManager, this);
m_authDialog = new AuthDialog(this);
ui = new Ui::MainWindow;
ui->setupUi(this);
@@ -87,29 +87,16 @@ MainWindow::MainWindow(QWidget *parent)
m_authDialog->setModal(true);
QTimer::singleShot(3000, this, [this]() {
if (!m_authManager->isSignedIn())
{
m_authDialog->reset();
m_authDialog->show();
}
});
connect(m_authManager, &AuthManager::codeSentToEmail, m_authDialog, &AuthDialog::onCodeSentToEmail);
connect(m_authManager, &AuthManager::errorOccurred, m_authDialog, &AuthDialog::showError);
connect(m_authManager, &AuthManager::signedIn, m_authDialog, &AuthDialog::hide);
connect(m_authDialog, &AuthDialog::codeRequested, m_authManager, &AuthManager::requestSignInCode);
connect(m_authDialog, &AuthDialog::signInRequested, m_authManager, &AuthManager::signIn);
connect(m_authManager, &AuthManager::signedIn, m_authDialog, &AuthDialog::reset);
connect(m_authManager, &AuthManager::signedOut, m_authDialog, &AuthDialog::reset);
connect(m_authManager, &AuthManager::signedOut, m_authDialog, &AuthDialog::show);
// Connect signal so dialog hides if valid session found, shows and resets on sign out
connect(m_authManager, &AuthManager::isSignedInChanged, this, [this]() {
if (m_authManager->isSignedIn())
{
m_authDialog->hide();
}
else
{
m_authDialog->reset();
m_authDialog->show();
}
});
// Check for cached session (async)
m_authManager->init();
m_authManager->tryRestoreSession();
setupTrayIcon();
}
+36 -27
View File
@@ -2,23 +2,15 @@
#include "ui_authdialog.h"
#include "authmanager.h"
AuthDialog::AuthDialog(AuthManager *authManager, QWidget *parent)
: QDialog{parent}, m_authManager(authManager), ui(new Ui::Dialog)
AuthDialog::AuthDialog(QWidget *parent)
: QDialog{parent}, ui(new Ui::Dialog)
{
ui->setupUi(this);
// 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 AuthManager signals
connect(m_authManager, &AuthManager::codeSentToEmail, this, [this](const QString &) {
showCodePage();
});
connect(m_authManager, &AuthManager::errorOccurred, this, [this](const QString &message) {
setError(message);
});
connect(ui->goBackButton, &QPushButton::clicked, this, &AuthDialog::showEmailPage);
// Start on email page
showEmailPage();
@@ -33,6 +25,7 @@ void AuthDialog::reset()
{
ui->emailLineEdit->clear();
ui->codeLineEdit->clear();
clearError();
showEmailPage();
}
@@ -40,26 +33,37 @@ void AuthDialog::showEmailPage()
{
ui->stackedWidget->setCurrentIndex(0);
ui->emailLineEdit->setFocus();
setError("");
clearError();
}
void AuthDialog::showCodePage()
{
ui->stackedWidget->setCurrentIndex(1);
ui->codeLineEdit->setFocus();
setError("");
clearError();
}
void AuthDialog::setError(const QString &message)
void AuthDialog::clearError()
{
// Update error labels on both pages
ui->errorMessage->setText(message);
ui->codeErrorMessage->setText(message);
ui->errorMessage->setText("");
ui->errorMessage->setVisible(false);
ui->codeErrorMessage->setText("");
ui->codeErrorMessage->setVisible(false);
}
// Show/hide based on whether there's an error
bool hasError = !message.isEmpty();
ui->errorMessage->setVisible(hasError);
ui->codeErrorMessage->setVisible(hasError);
void AuthDialog::showError(const QString &message)
{
if (ui->stackedWidget->currentIndex() == 0)
{
// Update error labels on both pages
ui->errorMessage->setText(message);
ui->errorMessage->setVisible(true);
}
else
{
ui->codeErrorMessage->setText(message);
ui->codeErrorMessage->setVisible(true);
}
}
void AuthDialog::onSendCodeClicked()
@@ -67,12 +71,17 @@ void AuthDialog::onSendCodeClicked()
QString email = ui->emailLineEdit->text().trimmed();
if (email.isEmpty())
{
setError("Please enter your email");
showError("Please enter your email");
return;
}
setError("");
m_authManager->requestSignInCode(email);
clearError();
emit codeRequested(email);
}
void AuthDialog::onCodeSentToEmail(const QString &email)
{
showCodePage();
}
void AuthDialog::onSignInClicked()
@@ -80,10 +89,10 @@ void AuthDialog::onSignInClicked()
QString code = ui->codeLineEdit->text().trimmed();
if (code.isEmpty())
{
setError("Please enter the code");
showError("Please enter the code");
return;
}
setError("");
m_authManager->signIn(code);
clearError();
emit signInRequested(code);
}
+10 -5
View File
@@ -3,8 +3,6 @@
#include <QDialog>
class AuthManager;
namespace Ui {
class Dialog;
}
@@ -13,22 +11,29 @@ class AuthDialog : public QDialog
{
Q_OBJECT
public:
explicit AuthDialog(AuthManager *authManager, QWidget *parent = nullptr);
explicit AuthDialog(QWidget *parent = nullptr);
~AuthDialog();
public slots:
void reset();
void showError(const QString &message);
void onCodeSentToEmail(const QString &email);
private slots:
void onSendCodeClicked();
void onSignInClicked();
signals:
void codeRequested(const QString &email);
void signInRequested(const QString &code);
private:
Ui::Dialog *ui;
AuthManager *m_authManager;
void showEmailPage();
void showCodePage();
void setError(const QString &message);
void clearError();
};
#endif // AUTHDIALOG_H
+11 -1
View File
@@ -31,7 +31,7 @@ AuthManager::AuthManager(QObject *parent)
});
}
void AuthManager::init()
void AuthManager::tryRestoreSession()
{
QString storageSessionToken = m_settings->value(key).toString();
if (storageSessionToken.isEmpty())
@@ -90,6 +90,15 @@ void AuthManager::setIsSignedIn(bool newValue)
m_isSignedIn = newValue;
emit isSignedInChanged();
if (m_isSignedIn)
{
emit signedIn();
}
else
{
emit signedOut();
}
}
bool AuthManager::isSignedIn() const
@@ -162,6 +171,7 @@ void AuthManager::signIn(const QString &code)
if (jsonDoc.isNull())
{
qWarning() << "Failed to create a JSON doc.";
emit errorOccurred("Unable to verify code");
return;
}
+6 -1
View File
@@ -42,10 +42,15 @@ public slots:
void signOut();
/// @brief checks if there is a cached session that is valid
void init();
void tryRestoreSession();
signals:
void sessionRestoreCompleted();
void isSignedInChanged();
void signedOut();
void signedIn();
void codeSentToEmail(const QString &email);
void errorOccurred(const QString &message);
+1
View File
@@ -9,6 +9,7 @@ void TestAuthManager::add()
{
}
// FIX: invalid test without access to emailed code
void TestAuthManager::testSignIn()
{
QSignalSpy codeSent(m_authManager, &AuthManager::codeSentToEmail);