Skip to content

Commit

Permalink
feat: add 2FA token field in login dialog
Browse files Browse the repository at this point in the history
Closes #6
  • Loading branch information
Octol1ttle committed Nov 12, 2023
1 parent aeea75b commit 0ad459a
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 2 deletions.
14 changes: 13 additions & 1 deletion launcher/ui/dialogs/LoginDialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

#include "minecraft/auth/AccountTask.h"

#include <QRegExp>
#include <QtWidgets/QPushButton>

LoginDialog::LoginDialog(QWidget* parent) : QDialog(parent), ui(new Ui::LoginDialog)
Expand All @@ -43,7 +44,11 @@ void LoginDialog::accept()

// Setup the login task and start it
m_account = MinecraftAccount::createFromUsername(ui->userTextBox->text());
m_loginTask = m_account->login(ui->passTextBox->text());
if (!ui->twofaTextBox->text().isEmpty()) {
m_loginTask = m_account->login(ui->passTextBox->text() + ":" + ui->twofaTextBox->text());
} else {
m_loginTask = m_account->login(ui->passTextBox->text());
}
connect(m_loginTask.get(), &Task::failed, this, &LoginDialog::onTaskFailed);
connect(m_loginTask.get(), &Task::succeeded, this, &LoginDialog::onTaskSucceeded);
connect(m_loginTask.get(), &Task::status, this, &LoginDialog::onTaskStatus);
Expand All @@ -55,6 +60,7 @@ void LoginDialog::setUserInputsEnabled(bool enable)
{
ui->userTextBox->setEnabled(enable);
ui->passTextBox->setEnabled(enable);
ui->twofaTextBox->setEnabled(enable);
ui->buttonBox->setEnabled(enable);
}

Expand All @@ -67,6 +73,12 @@ void LoginDialog::on_passTextBox_textEdited(const QString& newText)
{
ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(!newText.isEmpty() && !ui->userTextBox->text().isEmpty());
}
void LoginDialog::on_twofaTextBox_textEdited(const QString& newText)
{
QRegExp re("\\d*");
ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled((newText.isEmpty() || ui->twofaTextBox->text().isEmpty())
|| (ui->twofaTextBox->text().length() == 6 && re.exactMatch(ui->twofaTextBox->text())));
}

void LoginDialog::onTaskFailed(const QString& reason)
{
Expand Down
1 change: 1 addition & 0 deletions launcher/ui/dialogs/LoginDialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ class LoginDialog : public QDialog {

void on_userTextBox_textEdited(const QString& newText);
void on_passTextBox_textEdited(const QString& newText);
void on_twofaTextBox_textEdited(const QString& newText);

private:
Ui::LoginDialog* ui;
Expand Down
7 changes: 7 additions & 0 deletions launcher/ui/dialogs/LoginDialog.ui
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,13 @@
</property>
</widget>
</item>
<item>
<widget class="QLineEdit" name="twofaTextBox">
<property name="placeholderText">
<string>2FA token (if present)</string>
</property>
</widget>
</item>
<item>
<widget class="QProgressBar" name="progressBar">
<property name="value">
Expand Down
2 changes: 1 addition & 1 deletion launcher/ui/pages/global/AccountListPage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ void AccountListPage::listChanged()
void AccountListPage::on_actionAddMojang_triggered()
{
MinecraftAccountPtr account =
LoginDialog::newAccount(this, tr("Please enter your Ely.by account email and password to add your account."));
LoginDialog::newAccount(this, tr("Please enter your Ely.by account email, password and 2FA token (if present) to add your account."));

if (account) {
m_accounts->addAccount(account);
Expand Down

0 comments on commit 0ad459a

Please sign in to comment.