Support Forge Server Login account management and registration prompts

This commit is contained in:
Aryan Singh committed 2026-10-16 20:44:39 -05:00
1 parent 683cdc4016
commit d0a696f013
5 files changed
+59 -7

No files matched your search

+22
View File
@@ -384,9 +384,30 @@ export async function kickAll(reason: string): Promise<number> {
// ── EasyAuth ────────────────────────────────────────────────────────────
export function authProvider() {
return fs.existsSync(path.join(env.mcDataDir, "config", "serverlogin-common.toml")) ? "serverlogin" : "easyauth";
}
export function registrationCommand() {
return authProvider() === "serverlogin" ? "/login <password> <password>" : "/register <password> <password>";
}
export function parseServerLoginAccounts(text: string): string[] {
const data = JSON.parse(text) as { formatVersion?: number; users?: Record<string, { lastKnownName?: string }> };
if (data.formatVersion !== 1 || !data.users || Array.isArray(data.users) || typeof data.users !== "object") throw new Error("Unsupported Server Login account database");
return Object.values(data.users).map(account => account?.lastKnownName).filter((name): name is string => typeof name === "string" && isValidUsername(name));
}
/** Registered account names according to EasyAuth. */
export async function easyauthList(): Promise<string[]> {
if (authProvider() === "serverlogin") {
// Confirm the server is reachable before reconciling registration windows.
await execChecked("auth count");
const file = path.join(env.mcDataDir, "world", "serverlogin", "users.json");
if (!fs.existsSync(file)) return [];
return parseServerLoginAccounts(fs.readFileSync(file, "utf8"));
}
const out = stripColors(await rcon().exec("auth list"));
if (/unknown|incorrect|expected/i.test(out)) throw new Error("Authentication provider did not return an account list");
// Output is a list of registered usernames; extract all name-like tokens
// after a colon if present, otherwise from the whole string.
const colon = out.indexOf(":");
@@ -404,6 +425,7 @@ export async function easyauthRemove(name: string): Promise<string> {
export async function easyauthSetPassword(name: string, password: string): Promise<string> {
assertValidUsername(name);
if (authProvider() === "serverlogin") throw new Error("Use Force re-register, then let the player choose a new password in-game");
if (!/^[\x21-\x7e]{1,64}$/.test(password)) throw new Error("Unsupported password characters");
return stripColors(await rcon().exec(`auth update ${name} ${password}`));
}