| 1 | // Multi-user store. Replaces the simple users array inside settings.json with a |
| 2 | // dedicated file (data/users.json) and keeps per-user settings (relayKey, |
| 3 | // model, anthropicApiKey, etc.) so each user can authenticate against their |
| 4 | // own LLM credentials while sharing the same relay infrastructure. |
| 5 | const fs = require('fs'); |
| 6 | const path = require('path'); |
| 7 | const bcrypt = require('bcryptjs'); |
| 8 | |
| 9 | const FILE = path.resolve(__dirname, '..', 'data', 'users.json'); |
| 10 | |
| 11 | function readAll() { |
| 12 | try { return JSON.parse(fs.readFileSync(FILE, 'utf8')); } |
| 13 | catch { return []; } |
| 14 | } |
| 15 | function writeAll(list) { |
| 16 | fs.mkdirSync(path.dirname(FILE), { recursive: true }); |
| 17 | fs.writeFileSync(FILE, JSON.stringify(list, null, 2)); |
| 18 | } |
| 19 | |
| 20 | function listUsers() { |
| 21 | return readAll().map(u => ({ |
| 22 | username: u.username, name: u.name || u.username, email: u.email || '', |
| 23 | isAdmin: !!u.isAdmin, |
| 24 | createdAt: u.createdAt, lastLoginAt: u.lastLoginAt, |
| 25 | })); |
| 26 | } |
| 27 | |
| 28 | function findUser(username) { |
| 29 | return readAll().find(u => u.username === username) || null; |
| 30 | } |
| 31 | |
| 32 | function userCount() { return readAll().length; } |
| 33 | |
| 34 | async function createUser({ username, password, name, email, isAdmin }) { |
| 35 | if (!username || !/^[a-z0-9._-]+$/i.test(username)) throw new Error('username invalid'); |
| 36 | if (!password || password.length < 6) throw new Error('password must be ≥6 chars'); |
| 37 | const all = readAll(); |
| 38 | if (all.some(u => u.username.toLowerCase() === username.toLowerCase())) { |
| 39 | throw new Error('username already taken'); |
| 40 | } |
| 41 | const passwordHash = await bcrypt.hash(password, 10); |
| 42 | const user = { |
| 43 | username, |
| 44 | passwordHash, |
| 45 | name: name || username, |
| 46 | email: email || '', |
| 47 | isAdmin: !!isAdmin, |
| 48 | settings: {}, |
| 49 | createdAt: new Date().toISOString(), |
| 50 | }; |
| 51 | all.push(user); |
| 52 | writeAll(all); |
| 53 | return safeView(user); |
| 54 | } |
| 55 | |
| 56 | async function verifyPassword(username, password) { |
| 57 | const u = findUser(username); |
| 58 | if (!u) return null; |
| 59 | const ok = u.passwordHash |
| 60 | ? await bcrypt.compare(password, u.passwordHash) |
| 61 | : password === u.password; |
| 62 | if (!ok) return null; |
| 63 | // touch lastLoginAt |
| 64 | const all = readAll(); |
| 65 | const i = all.findIndex(x => x.username === username); |
| 66 | if (i >= 0) { |
| 67 | all[i].lastLoginAt = new Date().toISOString(); |
| 68 | writeAll(all); |
| 69 | } |
| 70 | return safeView(u); |
| 71 | } |
| 72 | |
| 73 | async function changePassword(username, newPassword) { |
| 74 | if (!newPassword || newPassword.length < 6) throw new Error('password must be ≥6 chars'); |
| 75 | const all = readAll(); |
| 76 | const i = all.findIndex(u => u.username === username); |
| 77 | if (i < 0) throw new Error('user not found'); |
| 78 | all[i].passwordHash = await bcrypt.hash(newPassword, 10); |
| 79 | delete all[i].password; |
| 80 | writeAll(all); |
| 81 | return safeView(all[i]); |
| 82 | } |
| 83 | |
| 84 | function deleteUser(username) { |
| 85 | const all = readAll().filter(u => u.username !== username); |
| 86 | writeAll(all); |
| 87 | } |
| 88 | |
| 89 | function getUserSettings(username) { |
| 90 | const u = findUser(username); |
| 91 | return u?.settings || {}; |
| 92 | } |
| 93 | |
| 94 | function updateUserSettings(username, patch) { |
| 95 | const all = readAll(); |
| 96 | const i = all.findIndex(u => u.username === username); |
| 97 | if (i < 0) throw new Error('user not found'); |
| 98 | all[i].settings = { ...(all[i].settings || {}), ...patch }; |
| 99 | writeAll(all); |
| 100 | return all[i].settings; |
| 101 | } |
| 102 | |
| 103 | function setAdmin(username, isAdmin) { |
| 104 | const all = readAll(); |
| 105 | const i = all.findIndex(u => u.username === username); |
| 106 | if (i < 0) throw new Error('user not found'); |
| 107 | all[i].isAdmin = !!isAdmin; |
| 108 | writeAll(all); |
| 109 | return safeView(all[i]); |
| 110 | } |
| 111 | |
| 112 | function findUserByEmail(email) { |
| 113 | return readAll().find(u => u.email && u.email.toLowerCase() === email.toLowerCase()) || null; |
| 114 | } |
| 115 | |
| 116 | function setResetToken(username, token) { |
| 117 | const all = readAll(); |
| 118 | const i = all.findIndex(u => u.username === username); |
| 119 | if (i < 0) return; |
| 120 | all[i].resetToken = token; |
| 121 | all[i].resetTokenExp = new Date(Date.now() + 3600_000).toISOString(); // 1h |
| 122 | writeAll(all); |
| 123 | } |
| 124 | |
| 125 | function findUserByResetToken(token) { |
| 126 | const u = readAll().find(u => u.resetToken === token); |
| 127 | if (!u) return null; |
| 128 | if (new Date(u.resetTokenExp) < new Date()) return null; |
| 129 | return u; |
| 130 | } |
| 131 | |
| 132 | function clearResetToken(username) { |
| 133 | const all = readAll(); |
| 134 | const i = all.findIndex(u => u.username === username); |
| 135 | if (i < 0) return; |
| 136 | delete all[i].resetToken; |
| 137 | delete all[i].resetTokenExp; |
| 138 | writeAll(all); |
| 139 | } |
| 140 | |
| 141 | function updateUserPlan(username, { plan, stripeCustomerId, stripeSubscriptionId, subscriptionStatus, planExpiresAt }) { |
| 142 | const all = readAll(); |
| 143 | const i = all.findIndex(u => u.username === username); |
| 144 | if (i < 0) throw new Error('user not found'); |
| 145 | if (plan !== undefined) all[i].plan = plan; |
| 146 | if (stripeCustomerId !== undefined) all[i].stripeCustomerId = stripeCustomerId; |
| 147 | if (stripeSubscriptionId !== undefined) all[i].stripeSubscriptionId = stripeSubscriptionId; |
| 148 | if (subscriptionStatus !== undefined) all[i].subscriptionStatus = subscriptionStatus; |
| 149 | if (planExpiresAt !== undefined) all[i].planExpiresAt = planExpiresAt; |
| 150 | writeAll(all); |
| 151 | return safeView(all[i]); |
| 152 | } |
| 153 | |
| 154 | function safeView(u) { |
| 155 | if (!u) return null; |
| 156 | return { |
| 157 | username: u.username, name: u.name || u.username, email: u.email || '', |
| 158 | isAdmin: !!u.isAdmin, |
| 159 | plan: u.plan || 'free', |
| 160 | stripeCustomerId: u.stripeCustomerId || null, |
| 161 | subscriptionStatus: u.subscriptionStatus || null, |
| 162 | planExpiresAt: u.planExpiresAt || null, |
| 163 | settings: u.settings || {}, |
| 164 | createdAt: u.createdAt, lastLoginAt: u.lastLoginAt, |
| 165 | }; |
| 166 | } |
| 167 | |
| 168 | // Migrate legacy users defined inside settings.json → users.json |
| 169 | async function migrateFromLegacy(legacyUsers) { |
| 170 | if (!Array.isArray(legacyUsers) || legacyUsers.length === 0) return 0; |
| 171 | const all = readAll(); |
| 172 | const have = new Set(all.map(u => u.username)); |
| 173 | let migrated = 0; |
| 174 | for (const lu of legacyUsers) { |
| 175 | if (have.has(lu.username)) continue; |
| 176 | all.push({ |
| 177 | username: lu.username, |
| 178 | passwordHash: lu.passwordHash, |
| 179 | name: lu.name || lu.username, |
| 180 | email: lu.email || '', |
| 181 | isAdmin: lu.isAdmin === undefined ? true : !!lu.isAdmin, // legacy users were admins |
| 182 | settings: {}, |
| 183 | createdAt: new Date().toISOString(), |
| 184 | }); |
| 185 | migrated++; |
| 186 | } |
| 187 | if (migrated) writeAll(all); |
| 188 | return migrated; |
| 189 | } |
| 190 | |
| 191 | module.exports = { |
| 192 | listUsers, findUser, findUserByEmail, createUser, verifyPassword, changePassword, |
| 193 | deleteUser, getUserSettings, updateUserSettings, setAdmin, userCount, |
| 194 | setResetToken, findUserByResetToken, clearResetToken, |
| 195 | updateUserPlan, migrateFromLegacy, safeView, |
| 196 | }; |