diff --git a/libnymea-app-core/connection/awsclient.cpp b/libnymea-app-core/connection/awsclient.cpp index f6a22169..d66e59b4 100644 --- a/libnymea-app-core/connection/awsclient.cpp +++ b/libnymea-app-core/connection/awsclient.cpp @@ -131,8 +131,14 @@ bool AWSClient::confirmationPending() const return m_confirmationPending; } -void AWSClient::login(const QString &username, const QString &password) +void AWSClient::login(const QString &username, const QString &password, int attempt) { + if (m_loginInProgress) { + qWarning() << "Login already pending..."; + return; + } + m_loginInProgress = true; + m_username = username; // Due to an issue in AWS apis it's very complex to use the refresh token. Taking a shortcut here for now: // Will store the password in the config for now and re-login when the accessToken expires. @@ -169,10 +175,15 @@ void AWSClient::login(const QString &username, const QString &password) qDebug() << "Logging in to AWS as user:" << username; QNetworkReply *reply = m_nam->post(request, payload); - connect(reply, &QNetworkReply::finished, this, [this, reply]() { + connect(reply, &QNetworkReply::finished, this, [this, reply, username, password, attempt]() { reply->deleteLater(); + m_loginInProgress = false; if (reply->error() != QNetworkReply::NoError) { qWarning() << "Error logging in to aws:" << reply->error() << reply->errorString(); + if (attempt < 3) { + login(username, password, attempt+1); + return; + } m_username.clear(); m_password.clear(); emit loginResult(LoginErrorUnknownError); diff --git a/libnymea-app-core/connection/awsclient.h b/libnymea-app-core/connection/awsclient.h index ba86becf..3ef89562 100644 --- a/libnymea-app-core/connection/awsclient.h +++ b/libnymea-app-core/connection/awsclient.h @@ -103,7 +103,7 @@ public: AWSDevices* awsDevices() const; bool confirmationPending() const; - Q_INVOKABLE void login(const QString &username, const QString &password); + Q_INVOKABLE void login(const QString &username, const QString &password, int attempt = -1); Q_INVOKABLE void logout(); Q_INVOKABLE void signup(const QString &username, const QString &password); Q_INVOKABLE void confirmRegistration(const QString &code); @@ -154,6 +154,8 @@ private: QString m_username; QString m_password; + bool m_loginInProgress = false; + bool m_confirmationPending = false; QByteArray m_accessToken;