Upstream/server/commands/signup.js
jvanvik b56b13880e Stage 0-2 server spine: db, scheduler, REST, auth, economy, map
Built: SQLite (node:sqlite) via server/db.js with checksummed migrations, injectable clock, persistent event
scheduler with catch-up, tiny REST router (error envelope, idempotency keys, rate limits, bearer tokens with scrypt
passwords), lazy-resource economy in public/js/rules.js + data.js, build/cancel/rename commands, seeded map +
spawn ring, generated docs/openapi.json. NETWALL net.js/static.js reused.
Tested: manual smoke run only (signup, town view, build, map). No unit tests or e2e yet.
NOT done: military, expansion, social, client, push, deploy docs, bots/admin.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Q2MJaQ5LXAHzGnTDgceT14
2026-10-11 18:07:42 +02:00

18 lines
1.2 KiB
JavaScript

'use strict';
/* Create account + operator + capital POP + first token in one transaction. The password hash and token hash are
* computed by the route before the transaction (scrypt is async, a transaction never spans an await). */
const { GameError } = require('../errors');
const joinWorld = require('./joinWorld');
module.exports = function signup(ctx, { username, passHash, tokenHash, device, worldId }) {
const { db, now } = ctx;
if (db.get('SELECT 1 AS x FROM users WHERE username = ?', username)) throw new GameError('username_taken');
const w = worldId !== undefined ? db.get("SELECT id FROM worlds WHERE id = ? AND status = 'running'", worldId)
: db.get("SELECT id FROM worlds WHERE status = 'running' ORDER BY id LIMIT 1");
if (!w) throw new GameError('not_found', { what: 'world' });
const u = db.run('INSERT INTO users (username, pass_hash, created_at) VALUES (?, ?, ?)', username, passHash, now);
const userId = Number(u.lastInsertRowid);
const joined = joinWorld(ctx, { userId, worldId: w.id });
const t = db.run('INSERT INTO tokens (user_id, token_hash, device, created_at, last_used_at) VALUES (?, ?, ?, ?, ?)', userId, tokenHash, device || '', now, now);
return Object.assign({ userId, tokenId: Number(t.lastInsertRowid) }, joined);
};