fix: demo seed works with existing users (seed flag) + static INVITE_CODE registration path + mount backend src (zero-build updates)

This commit is contained in:
root
2026-10-16 18:31:42 +08:00
parent 03daa8acc4
commit 4cb7ccbf15
3 changed files with 24 additions and 14 deletions
+10 -8
View File
@@ -2,10 +2,10 @@
import crypto from 'node:crypto';
import { getDb } from './db.mjs';
export function register({ username, password, device_name, invite_code }) {
export function register({ username, password, device_name, invite_code, seed }) {
const d = getDb();
// 多用户管控:管理员颁发邀请码;env INVITE_CODE 作为静态兜底
const maxUsers = Number(process.env.MAX_USERS || 0);
const maxUsers = seed ? 0 : Number(process.env.MAX_USERS || 0); // seed 播种不受名额限制
if (maxUsers > 0) {
const cnt = d.prepare('SELECT COUNT(*) AS c FROM users').get().c;
if (cnt >= maxUsers) throw new Error('注册名额已满,请联系管理员');
@@ -14,16 +14,18 @@ export function register({ username, password, device_name, invite_code }) {
let isAdmin = 0;
if (userCount === 0) {
isAdmin = 1; // 首位注册者自动成为管理员
} else if (seed) {
// 内置演示账号播种:不要求邀请码
} else {
const code = String(invite_code || '').trim().toUpperCase();
if (code) {
const row = d.prepare('SELECT id, valid FROM invite_codes WHERE code=?').get(code);
if (!row || !row.valid) throw new Error('邀请码无效或已使用');
const envCode = String(process.env.INVITE_CODE || '').trim().toUpperCase();
const row = code ? d.prepare('SELECT id, valid FROM invite_codes WHERE code=?').get(code) : null;
if (row && row.valid) {
var __usedInviteId = row.id; // eslint-disable-line no-var
} else if (process.env.INVITE_CODE && invite_code === process.env.INVITE_CODE) {
// 静态兜底码
} else if (code && envCode && code === envCode) {
// 静态兜底码(env INVITE_CODE),与动态邀请码等价
} else {
throw new Error('需要邀请码,请联系管理员');
throw new Error(code ? '邀请码无效或已使用' : '需要邀请码,请联系管理员');
}
}
if (!/^[a-zA-Z0-9_-]{3,32}$/.test(username || '')) throw new Error('用户名需为 3-32 位字母数字-_');