From 7d7d7800b1d6bcdf58f4e74f902b79627d6f41a7 Mon Sep 17 00:00:00 2001 From: Muthu Kumar Date: Tue, 17 Jul 2018 13:44:38 +0530 Subject: [PATCH] [refactor] Move validation to validator --- app.js | 10 +++------- lib/validator.js | 11 ++++++++--- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/app.js b/app.js index e9474b3..4815471 100644 --- a/app.js +++ b/app.js @@ -31,19 +31,15 @@ sessions.history = []; const getSession = sessionFinder(sessions); -bot.use((ctx, next) => - validator(ctx) - .then(next) - .catch(responder.fail( - `Username Not authenticated!` - ))); - // get os info const home = os.homedir(); const hostname = os.hostname(); const username = os.userInfo().username; const defaultShell = os.platform() === 'win32' ? 'cmd.exe' : 'bash'; +// Validate bot's master +bot.use(validator); + bot.command('start', ctx => { const newProc = spawn(defaultShell, { diff --git a/lib/validator.js b/lib/validator.js index 3fa2007..743f7fc 100644 --- a/lib/validator.js +++ b/lib/validator.js @@ -1,10 +1,15 @@ const { path } = require('../util/index.js'); const config = require('../config.js'); +const responder = require('./responseHandler.js'); const validate = - ctx => { + (ctx, next) => { if(!path(['update', 'message', 'from', 'id'], ctx)) return Promise.reject(ctx); - return (ctx.update.message.from.id === config.masterID) ? Promise.resolve(ctx) : Promise.reject(ctx); - } + return ((ctx.update.message.from.id === config.masterID) + ? Promise.resolve(ctx) + : Promise.reject(ctx)) + .then(next) + .catch(responder.fail('Username not authenticated.')); + }; module.exports = validate;