From d2148df68c5b5ede8d74738cfdfe7a432a3411f1 Mon Sep 17 00:00:00 2001 From: Francesco Esposito <33671357+frab1t@users.noreply.github.com> Date: Wed, 20 Feb 2019 14:31:23 +0100 Subject: [PATCH] add authentication --- auth.js | 15 +++++++++++++++ server.js | 31 ++++++++++++++++++++++++------- 2 files changed, 39 insertions(+), 7 deletions(-) create mode 100644 auth.js diff --git a/auth.js b/auth.js new file mode 100644 index 0000000..fa0c70b --- /dev/null +++ b/auth.js @@ -0,0 +1,15 @@ +const config = require('./config'); + +function checkUser(userId) { + return config.app.authorizedUsers.includes(userId); +} + +function auth(userId, okAuth, koAuth) { + if (checkUser(userId)) { + okAuth(); + } else { + koAuth(); + } +} + +module.exports = auth; diff --git a/server.js b/server.js index 34b7617..fa2103d 100644 --- a/server.js +++ b/server.js @@ -1,36 +1,53 @@ const Telebot = require('telebot'); const config = require('./config'); const utils = require('./utils'); +const auth = require('./auth'); const bot = new Telebot({ token: config.app.tokenBot, usePlugins: ['askUser'], }); +bot.on('/*', (msg) => { + bot.sendMessage(msg.from.id, 'qualsiasi'); +}); bot.on('/start', (msg) => { - bot.sendMessage(msg.from.id, utils.templateStart(), { parseMode: 'Markdown' }); - bot.event('/help', msg); + auth(msg.from.id, () => { + bot.sendMessage(msg.from.id, utils.templateStart(), { parseMode: 'Markdown' }); + bot.event('/help', msg); + }, + () => bot.sendMessage(msg.from.id, utils.templateUnauthorizedUser(), { parseMode: 'Markdown' })); }); bot.on('/help', (msg) => { - bot.sendMessage(msg.from.id, utils.templateHelp(), { parseMode: 'Markdown' }); + auth(msg.from.id, () => { + bot.sendMessage(msg.from.id, utils.templateHelp(), { parseMode: 'Markdown' }); + }); }); bot.on('/list', (msg) => { - bot.sendMessage(msg.from.id, 'list template'); + auth(msg.from.id, () => { + bot.sendMessage(msg.from.id, 'list template'); + }); }); bot.on('/position', (msg) => { - bot.sendMessage(msg.from.id, 'position template'); + auth(msg.from.id, () => { + bot.sendMessage(msg.from.id, 'position template'); + }); }); bot.on('/add', (msg) => { - bot.sendMessage(msg.from.id, 'add'); + auth(msg.from.id, () => { + bot.sendMessage(msg.from.id, 'add'); + }); }); bot.on('/remove', (msg) => { - bot.sendMessage(msg.from.id, 'remove'); + auth(msg.from.id, () => { + bot.sendMessage(msg.from.id, 'remove'); + }); });