1
0
mirror of https://github.com/SKCraft/Launcher.git synced 2025-01-07 19:29:03 +01:00

Automatically show login dialog when session expires

Previously it just gave an unhelpful error message; now it pops up
the login dialog again. Also, the duplicated accounts bug should be
fixed!
This commit is contained in:
Henry Le Grys 2021-05-24 13:17:11 +01:00
parent f23d387609
commit b7be91bc9c
5 changed files with 43 additions and 11 deletions

View File

@ -7,11 +7,14 @@
package com.skcraft.launcher.auth;
import com.skcraft.launcher.LauncherException;
import lombok.Getter;
/**
* Thrown on authentication error.
*/
public class AuthenticationException extends LauncherException {
@Getter
private boolean invalidatedSession = false;
public AuthenticationException(String message, String localizedMessage) {
super(message, localizedMessage);
@ -21,6 +24,11 @@ public class AuthenticationException extends LauncherException {
super(message, message);
}
public AuthenticationException(String message, boolean invalidatedSession) {
super(message, message);
this.invalidatedSession = invalidatedSession;
}
public AuthenticationException(Throwable cause, String localizedMessage) {
super(cause, localizedMessage);
}

View File

@ -54,11 +54,13 @@ public class YggdrasilLoginService implements LoginService {
if (req.getResponseCode() != 200) {
ErrorResponse error = req.returnContent().asJson(ErrorResponse.class);
throw new AuthenticationException(error.getErrorMessage());
throw new AuthenticationException(error.getErrorMessage(), true);
} else {
AuthenticateResponse response = req.returnContent().asJson(AuthenticateResponse.class);
Profile profile = response.getSelectedProfile();
if (profile == null) return null; // Minecraft not owned
if (previous != null && previous.getAvatarImage() != null) {
profile.setAvatarImage(previous.getAvatarImage());
} else {

View File

@ -7,10 +7,7 @@ import com.skcraft.concurrency.ObservableFuture;
import com.skcraft.concurrency.ProgressObservable;
import com.skcraft.concurrency.SettableProgress;
import com.skcraft.launcher.Launcher;
import com.skcraft.launcher.auth.LoginService;
import com.skcraft.launcher.auth.OfflineSession;
import com.skcraft.launcher.auth.SavedSession;
import com.skcraft.launcher.auth.Session;
import com.skcraft.launcher.auth.*;
import com.skcraft.launcher.persistence.Persistence;
import com.skcraft.launcher.swing.LinedBoxPanel;
import com.skcraft.launcher.swing.SwingHelper;
@ -100,7 +97,7 @@ public class AccountSelectDialog extends JDialog {
Session newSession = LoginDialog.showLoginRequest(this, launcher);
if (newSession != null) {
launcher.getAccounts().add(newSession.toSavedSession());
launcher.getAccounts().update(newSession.toSavedSession());
setResult(newSession);
}
});
@ -157,7 +154,7 @@ public class AccountSelectDialog extends JDialog {
progress.set(SharedLocale.tr("login.loggingInStatus"), -1));
if (newSession != null) {
launcher.getAccounts().add(newSession.toSavedSession());
launcher.getAccounts().update(newSession.toSavedSession());
setResult(newSession);
}
@ -184,13 +181,22 @@ public class AccountSelectDialog extends JDialog {
@Override
public void onFailure(Throwable t) {
t.printStackTrace();
if (t instanceof AuthenticationException) {
if (((AuthenticationException) t).isInvalidatedSession()) {
// Just need to log in again
LoginDialog.ReloginDetails details = new LoginDialog.ReloginDetails(session.getUsername(), t.getLocalizedMessage());
Session newSession = LoginDialog.showLoginRequest(AccountSelectDialog.this, launcher, details);
setResult(newSession);
}
} else {
SwingHelper.showErrorDialog(AccountSelectDialog.this, t.getLocalizedMessage(), SharedLocale.tr("errorTitle"), t);
}
}
}, SwingExecutor.INSTANCE);
ProgressDialog.showProgress(this, future, SharedLocale.tr("login.loggingInTitle"),
SharedLocale.tr("login.loggingInStatus"));
SwingHelper.addErrorDialogCallback(this, future);
}
@RequiredArgsConstructor

View File

@ -19,6 +19,7 @@ import com.skcraft.launcher.persistence.Persistence;
import com.skcraft.launcher.swing.*;
import com.skcraft.launcher.util.SharedLocale;
import com.skcraft.launcher.util.SwingExecutor;
import lombok.Data;
import lombok.Getter;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
@ -28,6 +29,7 @@ import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.IOException;
import java.util.Optional;
import java.util.concurrent.Callable;
/**
@ -38,6 +40,7 @@ public class LoginDialog extends JDialog {
private final Launcher launcher;
@Getter private Session session;
private final JLabel message = new JLabel(SharedLocale.tr("login.defaultMessage"));
private final JTextField usernameText = new JTextField();
private final JPasswordField passwordText = new JPasswordField();
private final JButton loginButton = new JButton(SharedLocale.tr("login.login"));
@ -52,7 +55,7 @@ public class LoginDialog extends JDialog {
* @param owner the owner
* @param launcher the launcher
*/
public LoginDialog(Window owner, @NonNull Launcher launcher) {
public LoginDialog(Window owner, @NonNull Launcher launcher, Optional<ReloginDetails> reloginDetails) {
super(owner, ModalityType.DOCUMENT_MODAL);
this.launcher = launcher;
@ -72,6 +75,8 @@ public class LoginDialog extends JDialog {
dispose();
}
});
reloginDetails.ifPresent(details -> message.setText(details.message));
}
@SuppressWarnings("unchecked")
@ -80,6 +85,7 @@ public class LoginDialog extends JDialog {
loginButton.setFont(loginButton.getFont().deriveFont(Font.BOLD));
formPanel.addRow(message);
formPanel.addRow(new JLabel(SharedLocale.tr("login.idEmail")), usernameText);
formPanel.addRow(new JLabel(SharedLocale.tr("login.password")), passwordText);
buttonsPanel.setBorder(BorderFactory.createEmptyBorder(26, 13, 13, 13));
@ -144,7 +150,11 @@ public class LoginDialog extends JDialog {
}
public static Session showLoginRequest(Window owner, Launcher launcher) {
LoginDialog dialog = new LoginDialog(owner, launcher);
return showLoginRequest(owner, launcher, null);
}
public static Session showLoginRequest(Window owner, Launcher launcher, ReloginDetails reloginDetails) {
LoginDialog dialog = new LoginDialog(owner, launcher, Optional.ofNullable(reloginDetails));
dialog.setVisible(true);
return dialog.getSession();
}
@ -186,4 +196,9 @@ public class LoginDialog extends JDialog {
}
}
@Data
public static class ReloginDetails {
private final String username;
private final String message;
}
}

View File

@ -100,6 +100,7 @@ login.login=Login...
login.recoverAccount=Forgot your login?
login.playOffline=Play offline
login.title=Minecraft Login
login.defaultMessage=Log in with your Mojang account
login.idEmail=ID/Email\:
login.password=Password\:
login.noPasswordError=Please enter a password.