classSource
public class AuthManager

Constructors

public AuthManager()

Methods

public static void addStateChangeListener(LoginStateChangeListener listener)
Parameters
listenerLoginStateChangeListener
public static LoginState getLoginState()
Returns a thread safe snapshot of the current LoginState.

Safe to be called from any thread (e.g. UI polling loops).

Returns
LoginStateA copy of the current login state.
public static String getLoginStateJson()
Returns
StringThe current login state as a JSON string, which can be used for UI display or debugging purposes.

getSavedAccounts()

staticexperimentalSource
public static List<UUID> getSavedAccounts() throws IOException
Lists every account that has a saved session. Every successful login saves one, next to the single session used by loginWithSavedSession()
Returns
List<UUID>The UUIDs of all accounts with a saved session, no order
Throws
IOExceptionIf the session directory exists but can't be read
public static User getUser()
Returns
UserThe currently logged-in user, or null if no session is active.

Note that this does not check if the session is still valid.

public static boolean hasSessionSaved()
Returns
booleanIf there is a saved session on disk(uses the directories given in init(BaseDir)

Note that this does not check if the session is still valid.

public static void init(Path baseDir, EventBus eventBus)
Initializes the auth manager using the given directory.
Parameters
baseDirPathThe directory where the key and session files will be stored.
eventBusEventBusThe event bus to push event updates to
public static void init(Path BaseDir)
Parameters
BaseDirPath
public static void init(String launcherName, EventBus eventBus)
Initializes the auth manager using the default application data directory.

It is recommended to use init(Path baseDir) instead if you want full control over the storage location.

Parameters
launcherNameStringThe launcher name used to create the application directory
eventBusEventBusThe event bus to push event updates to
public static void init(String launcherName)
Parameters
launcherNameString
public static User loadSavedSession(UUID uuid)
Loads the saved session of one account without refreshing it, for when there is no network The access token may have expired, that's fine for singleplayer but online servers will refuse it Will not work if disk is on fire, though we're already working on a fire extinguisher
Parameters
uuidUUIDThe account to load
Returns
UserThe user from the saved session, or null if there is none
Throws
LoginFailedExceptionIf the session file exists but can't be decrypted or read
public static User login()
Starts the device-code login flow and blocks until the login completes.

While this method is running, the device verification URL and user code are published to the shared loginState as soon as they are issued by the provider. This allows a UI thread to call getLoginState() (or getLoginStateJson()) and display the URL/code before login() returns.

Typical use: call startDeviceCodeLoginAsync() from UI code and poll the state. Use this blocking method only in special cases where a background thread is guaranteed, and you explicitly want a synchronous result.

Example:

new Thread(AuthManager::login).start();
while (true) {
LoginState state = AuthManager.getLoginState();
if (state.userCode != null && state.verificationUri != null) {
System.out.println(state.verificationUri + " -> " + state.userCode);
break;
}
try { Thread.sleep(100); } catch (InterruptedException ignored) {}
}
Returns
User
public static User loginWithSavedSession()
Attempts to load a saved session from disk, refreshes it if possible, and returns the corresponding User.
Returns
UserThe logged-in user, or nul if no valid session could be found.
public static User loginWithSavedSession(UUID uuid)
Loads the saved session of one account and refreshes it, like loginWithSavedSession() does for the most recent one
Parameters
uuidUUIDThe account to log in with
Returns
UserThe logged in user, or null if there is no saved session for it or it could not be refreshed
public static boolean removeSavedSession(UUID uuid) throws IOException
Forgets an account: deletes its saved session, and session.enc too if that one belongs to it
Parameters
uuidUUIDThe account to remove
Returns
booleanTrue if a saved session was deleted
Throws
IOExceptionIf a session file can't be deleted
public static void removeStateChangeListener(LoginStateChangeListener listener)
Parameters
listenerLoginStateChangeListener
public static void resetLoginState()
Refreshes the login state.
public static String startDeviceCodeLoginAsync()
Starts the device-code login flow on a background thread and returns immediately.

Typical use: call this from UI code, then poll getLoginState() (or JSON) to show the verification URL and user code while the login is pending. Use login() only in special cases where a synchronous, blocking call on a dedicated background thread is preferred.

Returns
StringThe initial login state as JSON, usually STARTING or PENDING

Nested types