docs / javascript

Telegram Login via Node.JS

This module extends the passport.js library and implements authentication with Telegram Messenger using the OAuth 2.0 protocol.

By plugging into Passport, Telegram authentication can be easily and unobtrusively integrated into any application or framework that supports Connect-style middleware, including Express.

Attention: this project is unofficial and is not affiliated with Telegram.

Installation

Package is available on npm:

$ npm install passport-telegram

Usage

Getting credentials

First of all you need to sign in and register your application. Then you'll be able to get the APP_ID and APP_SECRET.

Configure Strategy

Applications must supply a verify callback which accepts an accessToken, refreshToken and service-specific profile, and then calls the done callback supplying a user, which should be set to false if the credentials are not valid. If an exception occured, err should be set.

Options:

Example

For more detailed example see the example folder.

passport.use(
    new TelegramStrategy({
        clientID: '123-456-789',
        clientSecret: 'shhh-its-a-secret'
        callbackURL: 'https://www.example.net/auth/telegram/callback'
    },
    function(accessToken, refreshToken, profile, done) {
        User.findOrCreate(..., function (err, user) {
            done(err, user);
        });
    }
));

Authenticate Requests

Use passport.authenticate(), specifying the 'telegram' strategy to authenticate requests.

For example, as route middleware in an Express application:

app.get('/auth/telegram', 
    passport.authenticate('telegram'),
    function(req, res) {
        // The request will be redirected to telepass.me for authentication,
        // so this function will not be called.
    }
);

app.get('/auth/telegram/callback', 
    passport.authenticate('telegram', { failureRedirect: '/login' }),
    function(req, res) {
        // Successful authentication, redirect home.
        res.redirect('/');
    }
);

Examples

For a complete, working example, refer to the login example.

Tests

$ npm install --dev$ make test

Comments