Handle empty matrix ID (regression from 9de1ec1)

fixes #259
This commit is contained in:
Konstantinos Sideris 2018-03-01 09:31:08 +02:00
parent f6c279f6f2
commit 735d508a29
3 changed files with 30 additions and 43 deletions

View File

@ -48,7 +48,7 @@ protected:
public slots: public slots:
// Displays errors produced during the login. // Displays errors produced during the login.
void loginError(QString error_message); void loginError(QString msg) { error_label_->setText(msg); }
private slots: private slots:
// Callback for the back button. // Callback for the back button.

View File

@ -151,40 +151,23 @@ LoginPage::LoginPage(QSharedPointer<MatrixClient> client, QWidget *parent)
connect(serverInput_, SIGNAL(editingFinished()), this, SLOT(onServerAddressEntered())); connect(serverInput_, SIGNAL(editingFinished()), this, SLOT(onServerAddressEntered()));
} }
void
LoginPage::loginError(QString error)
{
error_label_->setText(error);
}
bool
LoginPage::isMatrixIdValid()
{
auto matrix_id = matrixid_input_->text();
try {
parse<User>(matrix_id.toStdString());
return true;
} catch (const std::exception &e) {
return false;
}
return false;
}
void void
LoginPage::onMatrixIdEntered() LoginPage::onMatrixIdEntered()
{ {
error_label_->setText(""); error_label_->setText("");
if (!isMatrixIdValid()) { User user;
loginError("You have entered an invalid Matrix ID e.g @joe:matrix.org");
return; try {
} else if (password_input_->text().isEmpty()) { user = parse<User>(matrixid_input_->text().toStdString());
loginError(tr("Empty password")); } catch (const std::exception &e) {
return loginError("You have entered an invalid Matrix ID e.g @joe:matrix.org");
} }
QString homeServer = matrixid_input_->text().split(":").at(1); if (password_input_->text().isEmpty())
return loginError(tr("Empty password"));
QString homeServer = QString::fromStdString(user.hostname());
if (homeServer != inferredServerAddress_) { if (homeServer != inferredServerAddress_) {
serverInput_->hide(); serverInput_->hide();
serverLayout_->removeWidget(errorIcon_); serverLayout_->removeWidget(errorIcon_);
@ -251,17 +234,21 @@ LoginPage::onLoginButtonClicked()
{ {
error_label_->setText(""); error_label_->setText("");
if (!isMatrixIdValid()) { User user;
loginError("You have entered an invalid Matrix ID e.g @joe:matrix.org");
} else if (password_input_->text().isEmpty()) { try {
loginError("Empty password"); user = parse<User>(matrixid_input_->text().toStdString());
} else { } catch (const std::exception &e) {
QString user = matrixid_input_->text().split(":").at(0).split("@").at(1); return loginError("You have entered an invalid Matrix ID e.g @joe:matrix.org");
QString password = password_input_->text();
client_->setServer(serverInput_->text());
client_->login(user, password);
emit loggingIn();
} }
if (password_input_->text().isEmpty())
return loginError(tr("Empty password"));
client_->setServer(serverInput_->text());
client_->login(QString::fromStdString(user.localpart()), password_input_->text());
emit loggingIn();
} }
void void

View File

@ -123,11 +123,6 @@ MatrixClient::login(const QString &username, const QString &password) noexcept
int status_code = int status_code =
reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(); reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
if (reply->error()) {
emit loginError(reply->errorString());
return;
}
if (status_code == 403) { if (status_code == 403) {
emit loginError(tr("Wrong username or password")); emit loginError(tr("Wrong username or password"));
return; return;
@ -144,6 +139,11 @@ MatrixClient::login(const QString &username, const QString &password) noexcept
return; return;
} }
if (reply->error()) {
emit loginError(reply->errorString());
return;
}
try { try {
mtx::responses::Login login = mtx::responses::Login login =
nlohmann::json::parse(reply->readAll().data()); nlohmann::json::parse(reply->readAll().data());