Simple authentication middleware for https://github.com/idchlife/node-telegram-bot-api-middleware
So, what's the case for this middleware?
Imagine, that you have telegram bot. And you want it to become bot for you company, your friends and also somebody else.
So... You have some private data and just answering to commands for you is not enough. You want some kind of 'registration' of users in your system.
So, with this middleware you will get it! Beware, this is called SIMPLEauth
for a reason. It uses sqlite3 and limited functionality.
As you know (if not yet, visit mentioned above repository node-telegram-bot-api-middleware), middleware is used to extend context of your next processed function callback for message/another middleware. This one does extend your list of middlewares with useful methods, properties for authentication and registration.
Installation
npm i node-telegram-bot-api-middleware-simpleauth --save
Usage
const simpleauth = require('node-telegram-bot-api-simpleauth').createMiddleware({
databaseFilename: 'YOUR_CUSTOM_DATABASE_FILE.sqlite3'
});
const use = require('node-telegram-bot-api-middleware');
const response = use(simpleauth);
const bot = require('./bot');
bot.on('message', response(function* () {
this.simpleauth.user
this.simpleauth.isUserAuthenticated();
this.simpleauth.isCurrentUserAdmin();
yield this.simpleauth.makeCurrentUserAdminAsync();
yield this.simpleauth.registerCurrentTelegramUserWithCodeAsync(code);
yield this.simpleauth.doesAuthCodeExist(code);
yield this.simpleauth.isAuthCodeAlreadyUsed(code);
yield this.simpleauth.registerUser(username, firstName, lastName, chatId, code);
const code = yield this.simpleauth.generateAuthCode();
});
Check example, it will show middleware in action
For working example you can see examples/working-auth-bot.js
To test it you need to start it like this:
node examples/working-auth-bot.js YOUR_BOT_TOKEN
You can help with code, ideas and bugs by creating issues and pull requests.
Yes, this middleware may have some bugs and unexpected behaviour and you can help me to improve it!