Skip to content

Commit

Permalink
fix: more fixes and improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelHillcox committed Mar 20, 2024
1 parent 44669be commit 5e9547f
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 23 deletions.
1 change: 1 addition & 0 deletions src/core/errors/errorCodes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const Codes: Readonly<Record<string, string>> = {
'ftb-auth#1001': 'No active profiles found, an active profile is required',
'ftb-auth#1002': 'Attempted to refresh user profile but failed',
'ftb-auth#1003': 'Legacy Mojang accounts are no longer supported',
'ftb-auth#1004': 'Unable to validate profile data, please retry.',
};

export type ErrorCodes = keyof typeof Codes;
Expand Down
53 changes: 31 additions & 22 deletions src/utils/auth/authentication.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,31 +184,40 @@ export const preLaunchChecksValid = async (): Promise<LaunchCheckResult> => {

logger.debug(`Validating profile ${profile.username} with uuid ${profile.uuid} against Microsoft`,);

const isValid = await sendMessage("profiles.is-valid", {
profileUuid: profile.uuid,
});

logger.debug(`The users token is ${isValid.success ? 'valid' : 'invalid'}`);
if (!isValid?.success) {
logger.debug(`Found a profile that no longer can be validated. Trying to refresh`);
const refresh = await msAuthenticator.refresh(profile);

// Update the profiles
await store.dispatch('core/loadProfiles');
logger.debug(`The refresh was ${refresh.ok ? 'successful' : 'unsuccessful'}`);
try {
const isValid = await sendMessage("profiles.is-valid", {
profileUuid: profile.uuid,
});

logger.debug(`The users token is ${isValid.success ? 'valid' : 'invalid'}`);
if (!isValid?.success) {
logger.debug(`Found a profile that no longer can be validated. Trying to refresh`);
const refresh = await msAuthenticator.refresh(profile);

// Update the profiles
await store.dispatch('core/loadProfiles');
logger.debug(`The refresh was ${refresh.ok ? 'successful' : 'unsuccessful'}`);
return {
ok: refresh.ok,
allowOffline: !refresh.ok && !refresh.tryLoginAgain,
requiresSignIn: refresh.tryLoginAgain ?? false,
error: refresh.ok ? createError('ftb-auth#1002') : undefined,
};
}

logger.debug(`Successfully authenticated ${profile.username} with uuid ${profile.uuid}`);
return {
ok: refresh.ok,
allowOffline: !refresh.ok && !refresh.tryLoginAgain,
requiresSignIn: refresh.tryLoginAgain ?? false,
error: refresh.ok ? createError('ftb-auth#1002') : undefined,
ok: true,
requiresSignIn: false,
};
} catch (e) {
logger.error(`Failed to validate the profile ${profile.username} with uuid ${profile.uuid}`, e);
return {
ok: false,
requiresSignIn: false,
error: createError('ftb-auth#1004'),
};
}

logger.debug(`Successfully authenticated ${profile.username} with uuid ${profile.uuid}`);
return {
ok: true,
requiresSignIn: false,
};
};

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public static void attemptMigration() {
} catch (IOException e) {
LOGGER.error("Failed to delete old settings file.", e);
}
} catch (IOException e) {
} catch (Exception e) {
LOGGER.error("Failed to migrate settings file. Aborting migration process.", e);
LOGGER.info("Deleting old settings file. If possible");
try {
Expand Down Expand Up @@ -181,6 +181,11 @@ public SettingsMigrator(Map<String, String> oldSettings) {
* Migrate old settings to the new format.
*/
private SettingsData migrate() {
if (this.oldSettings == null) {
// Something failed, fallback. We likely read the file wrong or an empty file
return DEFAULT_SETTINGS;
}

return new SettingsData(
DEFAULT_SPEC,
getOrDefault("instanceLocation", Path::of, DEFAULT_SETTINGS.instanceLocation()),
Expand Down

0 comments on commit 5e9547f

Please sign in to comment.