diff --git a/src/main/java/com/atlauncher/gui/tabs/accounts/AccountsTab.java b/src/main/java/com/atlauncher/gui/tabs/accounts/AccountsTab.java index 410d633..d7566b3 100644 --- a/src/main/java/com/atlauncher/gui/tabs/accounts/AccountsTab.java +++ b/src/main/java/com/atlauncher/gui/tabs/accounts/AccountsTab.java @@ -61,6 +61,7 @@ import com.atlauncher.utils.ComboItem; import com.atlauncher.utils.OS; import com.atlauncher.utils.SkinUtils; +import com.atlauncher.utils.Utils; import com.atlauncher.viewmodel.base.IAccountsViewModel; import com.atlauncher.viewmodel.base.IAccountsViewModel.LoginPostResult; import com.atlauncher.viewmodel.base.IAccountsViewModel.LoginPreCheckResult; @@ -474,6 +475,16 @@ private void clearLogin() { */ @SuppressWarnings("unchecked") private void login() { + if (!Utils.isEntryValid(usernameField.getText())) { + DialogManager + .okDialog() + .setTitle(GetText.tr("Account Not Added")) + .setContent(GetText.tr("Invalid username.")) + .setType(DialogManager.ERROR) + .show(); + return; + } + // Pre check LoginPreCheckResult preCheckResult = viewModel.loginPreCheck(); if (preCheckResult instanceof LoginPreCheckResult.Exists) { diff --git a/src/main/java/com/atlauncher/utils/Utils.java b/src/main/java/com/atlauncher/utils/Utils.java index b5a6f4d..20f0fdf 100644 --- a/src/main/java/com/atlauncher/utils/Utils.java +++ b/src/main/java/com/atlauncher/utils/Utils.java @@ -1676,4 +1676,8 @@ public static boolean isDevelopment() { return false; } + + public static boolean isEntryValid(String entry) { + return !(entry == null || entry.isBlank()); + } } diff --git a/src/main/java/com/atlauncher/viewmodel/impl/AccountsViewModel.java b/src/main/java/com/atlauncher/viewmodel/impl/AccountsViewModel.java index 389b4e8..85b5a99 100644 --- a/src/main/java/com/atlauncher/viewmodel/impl/AccountsViewModel.java +++ b/src/main/java/com/atlauncher/viewmodel/impl/AccountsViewModel.java @@ -24,6 +24,7 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +import org.mini2Dx.gettext.GetText; import com.atlauncher.data.AbstractAccount; import com.atlauncher.data.LoginResponse; @@ -32,8 +33,10 @@ import com.atlauncher.data.OfflineAccount; import com.atlauncher.gui.dialogs.ChangeSkinDialog; import com.atlauncher.managers.AccountManager; +import com.atlauncher.managers.DialogManager; import com.atlauncher.managers.LogManager; import com.atlauncher.utils.Authentication; +import com.atlauncher.utils.Utils; import com.atlauncher.viewmodel.base.IAccountsViewModel; /** @@ -176,16 +179,27 @@ private void addNewMojangAccount(LoginResponse response) { pushNewAccounts(); } - private void editAccount(LoginResponse response) { + private LoginPostResult editAccount(LoginResponse response) { AbstractAccount account = getSelectedAccount(); - if (account instanceof MojangAccount) { + if (account instanceof MojangAccount && response.getRealAccountType() == "mojang") { MojangAccount mojangAccount = (MojangAccount) account; mojangAccount.username = loginUsername; mojangAccount.minecraftUsername = response.getAuth().getSelectedProfile().getName(); mojangAccount.uuid = response.getAuth().getSelectedProfile().getId().toString(); if (loginRemember) { + // probably not needed at all but just in case + if (!Utils.isEntryValid(loginPassword)) { + DialogManager + .okDialog() + .setTitle(GetText.tr("Account Not Added")) + .setContent(GetText.tr("Invalid password.")) + .setType(DialogManager.ERROR) + .show(); + return new LoginPostResult.Error("Invalid password."); + } + mojangAccount.setPassword(loginPassword); } else { mojangAccount.encryptedPassword = null; @@ -196,7 +210,7 @@ private void editAccount(LoginResponse response) { mojangAccount.store = response.getAuth().saveForStorage(); AccountManager.saveAccounts(); - } else if (account instanceof OfflineAccount) { + } else if (account instanceof OfflineAccount && response.getRealAccountType() == "offline") { OfflineAccount offlineAccount = (OfflineAccount) account; offlineAccount.username = loginUsername; @@ -204,10 +218,14 @@ private void editAccount(LoginResponse response) { offlineAccount.store = response.getAuth().saveForStorage(); AccountManager.saveAccounts(); + } else { + LogManager.warn("Did not edit any account"); + return new LoginPostResult.Error("Invalid credentials."); } LogManager.info("Edited Account " + account); pushNewAccounts(); + return new LoginPostResult.Edited(); } private LoginResponse loginResponse = null; @@ -223,9 +241,9 @@ public LoginPostResult loginPost() { return new LoginPostResult.Added(); } - editAccount(loginResponse); + LoginPostResult res = editAccount(loginResponse); invalidateClientToken(); - return new LoginPostResult.Edited(); + return res; } else if (loginResponse != null && loginResponse.getRealAccountType() == "offline") { if (selectedAccountIndex == -1) { addNewOfflineAccount(); @@ -233,9 +251,9 @@ public LoginPostResult loginPost() { return new LoginPostResult.Added(); } - editAccount(loginResponse); + LoginPostResult res = editAccount(loginResponse); invalidateClientToken(); - return new LoginPostResult.Edited(); + return res; } @@ -249,9 +267,11 @@ public int getSelectedIndex() { @Override public void login() { - if (loginPassword == null || loginPassword.isEmpty()) { + if (!Utils.isEntryValid(loginPassword)) { + // offline acc loginResponse = Authentication.checkAccount(loginUsername); } else { + // mojang acc loginResponse = Authentication.checkAccount(loginUsername, loginPassword, getClientToken()); }