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