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:
1 parent
d20b7f4249
commit
d043a8f0f8
23 files changed
+134
-32
No files matched your search
@@ -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}`;
|
||||
}
|
||||
@@ -50,7 +50,10 @@ export class LoginService implements Service {
|
||||
if (!login) return yuplayAnswer('LOGINERROR', {}, 'sign-in: empty login');
|
||||
|
||||
const result = this.#accounts.signIn(login, password);
|
||||
if (result.kind === 'wrong-password') return yuplayAnswer('LOGINERROR', {}, `sign-in ${login}: wrong password`);
|
||||
if (result.kind !== 'signed-in') {
|
||||
const problem = result.kind === 'unknown-login' ? 'account not registered' : 'wrong password';
|
||||
return yuplayAnswer('LOGINERROR', {}, `sign-in ${login}: ${problem}`);
|
||||
}
|
||||
|
||||
const { account } = result;
|
||||
const credentials = issueCredentials(account);
|
||||
@@ -67,7 +70,7 @@ export class LoginService implements Service {
|
||||
nickorig: account.nick,
|
||||
nicksfx: '',
|
||||
meta: {},
|
||||
}, `sign-in ${login} -> user ${account.userId}${result.registered ? ' (new account)' : ''}`);
|
||||
}, `sign-in ${login} -> user ${account.userId}`);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -30,7 +30,8 @@ const zh = {
|
||||
failed: '无法注册本地账号。',
|
||||
};
|
||||
|
||||
const chinese = navigator.language.toLowerCase().startsWith('zh');
|
||||
const language = new URLSearchParams(location.search).get('skin_lang') || navigator.language;
|
||||
const chinese = language.toLowerCase().startsWith('zh');
|
||||
const text = chinese ? zh : en;
|
||||
document.documentElement.lang = chinese ? 'zh-CN' : 'en';
|
||||
document.title = text.title;
|
||||
|
||||
Reference in new issue
Block a user