passport-telegram2 v1.0.6
Passport-Telegram2
The author of Passport-Telegram has not maintained the original module for a long time (8 years) and their website does not work. So, we've made our own solution from the scratch and battle-tested it.
We published it to NPM with a new name passport-telegram2
.
Passport strategy for authenticating with Telegram using the OAuth 2.0 API by Telegrauth.
This module lets you authenticate using Telegram in your Node.js applications. By plugging into Passport, Telegram authentication can be easily and unobtrusively integrated into any application or framework that supports Connect-style middleware, including Express.
Installation
$ npm install passport-telegram2
Usage
Configure Strategy
The Telegram authentication strategy authenticates users using a Telegram account and OAuth 2.0 tokens provided by Telegrauth.
So, the first step is to create a new project in the Telegrauth and to get API keys. It free and takes 10 seconds.
The strategy requires a verify
callback, which accepts
these credentials and calls done
providing a user, as well as options
specifying a client ID, client secret, and callback URL.
passport.use(
new TelegramStrategy(
{
clientID: TELEGRAUTH_CLIENT_ID,
clientSecret: TELEGRAUTH_CLIENT_SECRET,
callbackURL: "http://127.0.0.1:3000/auth/telegram/callback",
},
function (accessToken, refreshToken, profile, done) {
User.findOrCreate({ telegramId: profile.id }, function (err, user) {
return 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", { scope: ["user"] })
);
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
Credits
Original creators of passport-github2
: