feat(accounts): add explicit local account registration

This commit is contained in:
N0VAN0M4LY committed 2026-11-29 15:30:34 +08:00
1 parent ef3648cb13
commit 2b1da24263
8 files changed
+242 -6

No files matched your search

+1 -1
View File
@@ -24,7 +24,7 @@ import { StoreMediaCache, STORE_MEDIA_PREFIX } from './store-media-cache.ts';
import { contentResponse, createStaticWebSite, notFound } from './web-helpers.ts';
const PREFIX = '/_wto/ui/';
const SHARED_FILES = createStaticWebSite(new URL('./web/', import.meta.url), ['common.css', 'common.js', 'official-images.json', 'official-store-media.json']);
const SHARED_FILES = createStaticWebSite(new URL('./web/', import.meta.url), ['common.css', 'common.js', 'register.css', 'register.js', 'official-images.json', 'official-store-media.json']);
const ICON_PATH = /^(vehicle|flag)\/([^/]+)\.svg$/;
const ART_PATH = /^art\/(vehicle|group|product|skin|decal|attachable|unlock)\/([^/]+)$/;
const PACK_PREFIX = 'asset-pack/';
+33
View File
@@ -0,0 +1,33 @@
.registration {
width: 100%;
max-width: 480px;
margin: 24px auto;
padding: 24px;
}
.registration h1 {
margin: 0 0 6px;
font-size: 22px;
}
.registration form,
.registration label {
display: grid;
gap: 6px;
}
.registration form {
gap: 14px;
}
.registration input {
min-width: 0;
}
.registration button {
justify-self: start;
}
.registration .notice {
margin: 0;
}
+26
View File
@@ -0,0 +1,26 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Create a local account</title>
<link rel="stylesheet" href="/_wto/ui/common.css">
<link rel="stylesheet" href="/_wto/ui/register.css">
<script src="/_wto/ui/register.js" defer></script>
</head>
<body>
<main>
<section class="card registration">
<h1 data-text="title">Create a local account</h1>
<form id="register" method="post" action="/_wto/register">
<label><span data-text="login">Login</span><input name="login" autocomplete="username" required></label>
<label><span data-text="password">Password</span><input name="password" type="password" autocomplete="new-password" required></label>
<label><span data-text="confirm">Confirm password</span><input name="confirmation" type="password" autocomplete="new-password" required></label>
<label><span data-text="nick">Nickname</span><input name="nick" autocomplete="nickname" required></label>
<button class="buy" type="submit" data-text="create">Create account</button>
</form>
<p id="result" class="notice" role="status"></p>
</section>
</main>
</body>
</html>
+78
View File
@@ -0,0 +1,78 @@
const en = {
title: 'Create a local account',
login: 'Login',
password: 'Password',
confirm: 'Confirm password',
nick: 'Nickname',
create: 'Create account',
created: 'Account created. Return to the game to sign in.',
mismatch: 'The passwords do not match.',
'already-registered': 'An account with this login already exists.',
'invalid-login': 'Enter a login without spaces.',
'invalid-password': 'Enter a password.',
'invalid-nick': 'Enter a nickname with up to 16 characters.',
failed: 'The account could not be created.',
};
const zh = {
title: '注册本地账号',
login: '登录账号',
password: '密码',
confirm: '确认密码',
nick: '昵称',
create: '注册',
created: '账号已注册。请返回游戏登录。',
mismatch: '两次输入的密码不一致。',
'already-registered': '该登录账号已注册。',
'invalid-login': '登录账号不能包含空格。',
'invalid-password': '请输入密码。',
'invalid-nick': '请输入最多 16 个字符的昵称。',
failed: '无法注册本地账号。',
};
const chinese = navigator.language.toLowerCase().startsWith('zh');
const text = chinese ? zh : en;
document.documentElement.lang = chinese ? 'zh-CN' : 'en';
document.title = text.title;
for (const element of document.querySelectorAll('[data-text]')) element.textContent = text[element.dataset.text];
const form = document.getElementById('register');
const result = document.getElementById('result');
const submit = form.querySelector('button[type=submit]');
function show(message, success) {
result.className = success ? 'notice ok' : 'notice err';
result.textContent = message;
}
form.addEventListener('submit', async (event) => {
event.preventDefault();
const fields = new FormData(form);
const login = fields.get('login');
const password = fields.get('password');
const nick = fields.get('nick');
if (password !== fields.get('confirmation')) {
show(text.mismatch, false);
return;
}
submit.disabled = true;
try {
const response = await fetch(form.action, {
method: 'POST',
headers: { 'content-type': 'application/json' },
body: JSON.stringify({ login, password, nick }),
});
const body = await response.json();
if (response.ok) {
form.reset();
form.hidden = true;
show(text.created, true);
} else {
show(text[body.error] ?? text.failed, false);
}
} catch {
show(text.failed, false);
} finally {
submit.disabled = false;
}
});