diff --git a/src/server/models/user.model.js b/src/server/models/user.model.js index 5561b8c..53658fa 100644 --- a/src/server/models/user.model.js +++ b/src/server/models/user.model.js @@ -47,7 +47,7 @@ User.saltRounds = 10; export default User; registerHooks({ - replCommands: (socket) => ({ + replCommands: () => ({ createUser: (spec) => { const [email, maybePassword] = spec.split(' ', 2); const password = maybePassword || randomBytes(8).toString('hex'); @@ -58,13 +58,11 @@ registerHooks({ const password = randomBytes(8).toString('hex'); const user = await User.findOne({where: {email}}); if (user) { - user.addHashedPassword(password) + return user.addHashedPassword(password) .then(() => user.save()) - // eslint-disable-next-line no-console - .then(() => socket.write(`New password:\n\n${password}\n`)); + .then(() => `\nNew password: ${password}\n\n`); } - // eslint-disable-next-line no-console - console.log('User not found.'); + return 'User not found.\n'; }, }), }, module.id); diff --git a/src/server/repl.js b/src/server/repl.js index d248853..fa86cf8 100644 --- a/src/server/repl.js +++ b/src/server/repl.js @@ -16,9 +16,17 @@ export function createReplServer() { replServer.context[key] = value; }); Object.entries( - invokeHookFlat('replCommands', socket).reduce((r, commands) => ({...r, ...commands}), {}), + invokeHookFlat('replCommands').reduce((r, commands) => ({...r, ...commands}), {}), ).forEach(([key, value]) => { - replServer.defineCommand(key, value); + replServer.defineCommand(key, async (arg) => { + const result = await value(arg); + if (result) { + socket.write(result, () => replServer.displayPrompt()); + } + else { + replServer.displayPrompt(); + } + }); }); }); netServer.listen(`/tmp/reddichat-${Date.now()}.sock`);