From 3dbd4f220feb5c0fad80c77b2fdc9d6c56d1b2f8 Mon Sep 17 00:00:00 2001 From: Ryan Huffman Date: Wed, 2 Oct 2019 16:51:37 -0700 Subject: [PATCH] Add metaverse env var and login error message to qt launcher --- .../qt/resources/qml/HFBase/LoginBase.qml | 24 ++++++++++++++++-- launchers/qt/src/Helper.cpp | 9 +++++++ launchers/qt/src/Helper.h | 8 +----- launchers/qt/src/LauncherState.cpp | 25 +++++++++++-------- launchers/qt/src/LauncherState.h | 16 ++++-------- launchers/qt/src/LoginRequest.cpp | 15 ++++++++--- launchers/qt/src/LoginRequest.h | 1 + launchers/qt/src/SignupRequest.cpp | 4 +-- launchers/qt/src/UserSettingsRequest.cpp | 2 +- 9 files changed, 67 insertions(+), 37 deletions(-) diff --git a/launchers/qt/resources/qml/HFBase/LoginBase.qml b/launchers/qt/resources/qml/HFBase/LoginBase.qml index deb21ad085..fa0737be0e 100644 --- a/launchers/qt/resources/qml/HFBase/LoginBase.qml +++ b/launchers/qt/resources/qml/HFBase/LoginBase.qml @@ -42,6 +42,7 @@ Item { horizontalAlignment: Text.AlignHCenter verticalAlignment: Text.AlignVCenter color: "#C4C4C4" + visible: LauncherState.lastLoginErrorMessage.length == 0 text: "Use the account credentials you created at sign-up" anchors { left: root.left @@ -51,6 +52,25 @@ Item { } } + Text { + id: error + width: 425 + height: 22 + font.family: "Graphik" + font.pixelSize: 14 + horizontalAlignment: Text.AlignHCenter + verticalAlignment: Text.AlignVCenter + color: "#FF9999" + visible: LauncherState.lastLoginErrorMessage.length > 0 + text: LauncherState.lastLoginErrorMessage + anchors { + left: root.left + right: root.right + top: title.bottom + topMargin: 18 + } + } + HFTextField { id: username width: 353 @@ -61,8 +81,8 @@ Item { color: "#7E8C81" seperatorColor: Qt.rgba(1, 1, 1, 0.3) anchors { - top: instruction.bottom - horizontalCenter: instruction.horizontalCenter + top: error.bottom + horizontalCenter: error.horizontalCenter topMargin: 24 } } diff --git a/launchers/qt/src/Helper.cpp b/launchers/qt/src/Helper.cpp index 729824da3f..38e6040a16 100644 --- a/launchers/qt/src/Helper.cpp +++ b/launchers/qt/src/Helper.cpp @@ -6,8 +6,17 @@ #include #include #include +#include +QString getMetaverseAPIDomain() { + QProcessEnvironment processEnvironment = QProcessEnvironment::systemEnvironment(); + if (processEnvironment.contains("HIFI_METAVERSE_URL")) { + return processEnvironment.value("HIFI_METAVERSE_URL"); + } + return "https://metaverse.highfidelity.com"; +} + void swapLaunchers(const QString& oldLauncherPath, const QString& newLauncherPath) { if (!(QFileInfo::exists(oldLauncherPath) && QFileInfo::exists(newLauncherPath))) { qDebug() << "old launcher: " << oldLauncherPath << "new launcher: " << newLauncherPath << " file does not exist"; diff --git a/launchers/qt/src/Helper.h b/launchers/qt/src/Helper.h index a41b55b233..e5a3c4f502 100644 --- a/launchers/qt/src/Helper.h +++ b/launchers/qt/src/Helper.h @@ -5,13 +5,7 @@ #include "Windows.h" #endif -//#define USE_STAGING - -#ifdef USE_STAGING -const QString METAVERSE_API_DOMAIN{ "https://staging.highfidelity.com" }; -#else -const QString METAVERSE_API_DOMAIN{ "https://metaverse.highfidelity.com" }; -#endif +QString getMetaverseAPIDomain(); void launchClient(const QString& clientPath, const QString& homePath, const QString& defaultScriptOverride, const QString& displayName, const QString& contentCachePath, QString loginResponseToken = QString()); diff --git a/launchers/qt/src/LauncherState.cpp b/launchers/qt/src/LauncherState.cpp index 582b416b85..0d094d8419 100644 --- a/launchers/qt/src/LauncherState.cpp +++ b/launchers/qt/src/LauncherState.cpp @@ -67,6 +67,11 @@ bool LauncherState::shouldDownloadContentCache() const { return !_contentCacheURL.isNull() && !QFile::exists(getContentCachePath()); } +void LauncherState::setLastLoginErrorMessage(const QString& msg) { + _lastLoginErrorMessage = msg; + emit lastLoginErrorMessageChanged(); +} + static const std::array QML_FILE_FOR_UI_STATE = { { "qml/SplashScreen.qml", "qml/HFBase/CreateAccountBase.qml", "qml/HFBase/LoginBase.qml", "DisplayName.qml", "qml/Download.qml", "qml/DownloadFinished.qml", "qml/HFBase/Error.qml" } }; @@ -142,14 +147,6 @@ LauncherState::UIState LauncherState::getUIState() const { } } -void LauncherState::setLastLoginError(LastLoginError lastLoginError) { - _lastLoginError = lastLoginError; -} - -LauncherState::LastLoginError LauncherState::getLastLoginError() const { - return _lastLoginError; -} - void LauncherState::restart() { setApplicationState(ApplicationState::Init); requestBuilds(); @@ -296,7 +293,11 @@ void LauncherState::signup(QString email, QString username, QString password, QS loginRequest->deleteLater(); auto err = loginRequest->getError(); - if (err != LoginRequest::Error::None) { + if (err == LoginRequest::Error::BadUsernameOrPassword) { + setLastLoginErrorMessage("Bad username or password"); + setApplicationState(ApplicationState::WaitingForLogin); + return; + } else if (err != LoginRequest::Error::None) { setApplicationStateError("Failed to login"); return; } @@ -333,7 +334,11 @@ void LauncherState::login(QString username, QString password, QString displayNam request->deleteLater(); auto err = request->getError(); - if (err != LoginRequest::Error::None) { + if (err == LoginRequest::Error::BadUsernameOrPassword) { + setLastLoginErrorMessage("Bad username or password"); + setApplicationState(ApplicationState::WaitingForLogin); + return; + } else if (err != LoginRequest::Error::None) { setApplicationStateError("Failed to login"); return; } diff --git a/launchers/qt/src/LauncherState.h b/launchers/qt/src/LauncherState.h index 324a653854..37899486f1 100644 --- a/launchers/qt/src/LauncherState.h +++ b/launchers/qt/src/LauncherState.h @@ -25,6 +25,7 @@ class LauncherState : public QObject { Q_PROPERTY(ApplicationState applicationState READ getApplicationState NOTIFY applicationStateChanged) Q_PROPERTY(float downloadProgress READ getDownloadProgress NOTIFY downloadProgressChanged) Q_PROPERTY(SignupRequest::Error lastSignupError MEMBER _lastSignupError NOTIFY lastSignupErrorChanged); + Q_PROPERTY(QString lastLoginErrorMessage READ getLastLoginErrorMessage NOTIFY lastLoginErrorMessageChanged); public: LauncherState(); @@ -67,16 +68,8 @@ public: LaunchingHighFidelity }; - enum LastLoginError { - NONE = 0, - ORGINIZATION, - CREDENTIALS, - LAST_ERROR_NUM - }; - Q_ENUM(UIState); Q_ENUM(ApplicationState) - Q_ENUM(LastLoginError) Q_INVOKABLE QString getCurrentUISource() const; @@ -87,8 +80,8 @@ public: UIState getUIState() const; - void setLastLoginError(LastLoginError lastLoginError); - LastLoginError getLastLoginError() const; + void setLastLoginErrorMessage(const QString& msg); + QString getLastLoginErrorMessage() const { return _lastLoginErrorMessage; } void setApplicationStateError(QString errorMessage); void setApplicationState(ApplicationState state); @@ -134,6 +127,7 @@ signals: void applicationStateChanged(); void downloadProgressChanged(); void lastSignupErrorChanged(); + void lastLoginErrorMessageChanged(); private slots: void clientDownloadComplete(); @@ -161,8 +155,8 @@ private: // Application State ApplicationState _applicationState { ApplicationState::Init }; LoginToken _loginResponse; - LastLoginError _lastLoginError { NONE }; SignupRequest::Error _lastSignupError{ SignupRequest::Error::None }; + QString _lastLoginErrorMessage{ "" }; QString _displayName; QString _applicationErrorMessage; QString _currentClientVersion; diff --git a/launchers/qt/src/LoginRequest.cpp b/launchers/qt/src/LoginRequest.cpp index 488b5bbfa5..1c08847c6a 100644 --- a/launchers/qt/src/LoginRequest.cpp +++ b/launchers/qt/src/LoginRequest.cpp @@ -8,7 +8,7 @@ #include void LoginRequest::send(QNetworkAccessManager& nam, QString username, QString password) { - QNetworkRequest request(QUrl(METAVERSE_API_DOMAIN + "/oauth/token")); + QNetworkRequest request(QUrl(getMetaverseAPIDomain() + "/oauth/token")); request.setHeader(QNetworkRequest::UserAgentHeader, getHTTPUserAgent()); request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded"); @@ -28,11 +28,19 @@ void LoginRequest::receivedResponse() { auto reply = static_cast(sender()); - if (reply->error()) { + auto statusCode = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(); + + if (statusCode < 100) { qDebug() << "Error logging in: " << reply->readAll(); _error = Error::Unknown; emit finished(); - //setApplicationState(ApplicationState::UnexpectedError); + return; + } + + if (statusCode >= 500 && statusCode < 600) { + qDebug() << "Error logging in: " << reply->readAll(); + _error = Error::ServerError; + emit finished(); return; } @@ -44,7 +52,6 @@ void LoginRequest::receivedResponse() { qDebug() << "Error parsing response for login" << data; _error = Error::BadResponse; emit finished(); - //setApplicationStateError("Failed to login"); return; } diff --git a/launchers/qt/src/LoginRequest.h b/launchers/qt/src/LoginRequest.h index 07b28ff794..465963b62e 100644 --- a/launchers/qt/src/LoginRequest.h +++ b/launchers/qt/src/LoginRequest.h @@ -21,6 +21,7 @@ public: enum class Error { None = 0, Unknown, + ServerError, BadResponse, BadUsernameOrPassword }; diff --git a/launchers/qt/src/SignupRequest.cpp b/launchers/qt/src/SignupRequest.cpp index a6961c0094..0a2c554f8d 100644 --- a/launchers/qt/src/SignupRequest.cpp +++ b/launchers/qt/src/SignupRequest.cpp @@ -15,7 +15,7 @@ void SignupRequest::send(QNetworkAccessManager& nam, QString email, QString user _state = State::Sending; - QUrl signupURL { METAVERSE_API_DOMAIN }; + QUrl signupURL { getMetaverseAPIDomain() }; signupURL.setPath("/api/v1/user/channel_user"); QNetworkRequest request(signupURL); @@ -27,7 +27,7 @@ void SignupRequest::send(QNetworkAccessManager& nam, QString email, QString user query.addQueryItem("username", username); query.addQueryItem("password", password); - auto reply = nam.post(request, query.toString().toUtf8()); + auto reply = nam.put(request, query.toString().toUtf8()); QObject::connect(reply, &QNetworkReply::finished, this, &SignupRequest::receivedResponse); } diff --git a/launchers/qt/src/UserSettingsRequest.cpp b/launchers/qt/src/UserSettingsRequest.cpp index 62f7d5b4fb..737d103784 100644 --- a/launchers/qt/src/UserSettingsRequest.cpp +++ b/launchers/qt/src/UserSettingsRequest.cpp @@ -12,7 +12,7 @@ const QByteArray ACCESS_TOKEN_AUTHORIZATION_HEADER = "Authorization"; void UserSettingsRequest::send(QNetworkAccessManager& nam, const LoginToken& token) { _state = State::Sending; - QUrl lockerURL = METAVERSE_API_DOMAIN; + QUrl lockerURL{ getMetaverseAPIDomain() }; lockerURL.setPath("/api/v1/user/locker"); QNetworkRequest lockerRequest(lockerURL);