diff --git a/server/clientManager.ts b/server/clientManager.ts index a0f1aabf..bbab4f49 100644 --- a/server/clientManager.ts +++ b/server/clientManager.ts @@ -86,39 +86,28 @@ class ClientManager { } autoloadUsers() { - fs.watch( - Config.getUsersPath(), - _.debounce( - () => { - const loaded = this.clients.map((c) => c.name); - const updatedUsers = this.getUsers(); + fs.watch(Config.getUsersPath(), (_eventType, file) => { + if (!file.endsWith(".json")) { + return; + } - if (updatedUsers.length === 0) { - log.info( - `There are currently no users. Create one with ${colors.bold( - "thelounge add " - )}.` - ); - } + const name = file.slice(0, -5); - // Reload all users. Existing users will only have their passwords reloaded. - updatedUsers.forEach((name) => this.loadUser(name)); + const userPath = Config.getUserConfigPath(name); - // Existing users removed since last time users were loaded - _.difference(loaded, updatedUsers).forEach((name) => { - const client = _.find(this.clients, {name}); + if (fs.existsSync(userPath)) { + this.loadUser(name); + return; + } - if (client) { - client.quit(true); - this.clients = _.without(this.clients, client); - log.info(`User ${colors.bold(name)} disconnected and removed.`); - } - }); - }, - 1000, - {maxWait: 10000} - ) - ); + const client = _.find(this.clients, {name}); + + if (client) { + client.quit(true); + this.clients = _.without(this.clients, client); + log.info(`User ${colors.bold(name)} disconnected and removed.`); + } + }); } loadUser(name: string) { @@ -180,9 +169,11 @@ class ClientManager { }; try { - fs.writeFileSync(userPath, JSON.stringify(user, null, "\t"), { + const tmpPath = userPath + ".tmp"; + fs.writeFileSync(tmpPath, JSON.stringify(user, null, "\t"), { mode: 0o600, }); + fs.renameSync(tmpPath, userPath); } catch (e: any) { log.error(`Failed to create user ${colors.green(name)} (${e})`); throw e;