feat(accounts)!: require explicit local account registration

Open the local signup page in the game's embedded browser before sign-in. Return the existing login error for unknown accounts or invalid passwords.

BREAKING CHANGE: First-time players must register in the game's signup window or create a save in the launcher before signing in. Existing accounts can sign in with their saved credentials.
This commit is contained in:
N0VAN0M4LY committed 2026-11-29 18:00:21 +08:00
1 parent d20b7f4249
commit d043a8f0f8
23 files changed
+134 -32

No files matched your search

+8 -20
View File
@@ -1,8 +1,8 @@
/**
* Local accounts behind the login service.
*
* Signing in with a login name that does not exist yet registers it with the password given;
* later sign-ins must use the same password. Accounts are kept in <data>/accounts.json.
* Accounts are created through explicit registration. Sign-in verifies the credentials of
* an existing account. Accounts are kept in <data>/accounts.json.
*/
import { existsSync, readFileSync } from 'node:fs';
import { join } from 'node:path';
@@ -20,8 +20,8 @@ export interface Account {
}
export type SignInResult =
| { readonly kind: 'signed-in'; readonly account: Account; readonly registered: boolean }
| { readonly kind: 'wrong-password' };
| { readonly kind: 'signed-in'; readonly account: Account }
| { readonly kind: 'unknown-login' | 'wrong-password' };
export type RegistrationResult =
| { readonly kind: 'registered'; readonly account: Account }
@@ -47,16 +47,11 @@ export class AccountStore {
}
signIn(login: string, password: string): SignInResult {
const existing = this.#find(login);
if (existing) {
if (!verifyPassword(password, existing.passwordHash)) return { kind: 'wrong-password' };
this.#lastSignedInUserId = existing.userId;
return { kind: 'signed-in', account: existing, registered: false };
}
const account = this.#create(login, password, nickFromLogin(login, String(this.#file.nextUserId)));
const account = this.#find(login);
if (!account) return { kind: 'unknown-login' };
if (!verifyPassword(password, account.passwordHash)) return { kind: 'wrong-password' };
this.#lastSignedInUserId = account.userId;
return { kind: 'signed-in', account, registered: true };
return { kind: 'signed-in', account };
}
register(login: string, password: string, nick: string): RegistrationResult {
@@ -138,10 +133,3 @@ function readAccountFile(path: string): AccountFile {
if (file.formatVersion !== 1) throw new Error(`${path}: unsupported account file version ${String(file.formatVersion)}`);
return file;
}
/** A login is often an e-mail address; the nick is its readable part. */
function nickFromLogin(login: string, userId: string): string {
const local = login.split('@')[0] ?? '';
const cleaned = local.replace(/[^A-Za-z0-9_\-]/g, '').slice(0, NICK_MAX_LENGTH);
return cleaned || `Player${userId}`;
}