passport-twitter-2
Version:
Passport Strategy for Twitter API v2
63 lines (48 loc) • 1.4 kB
Markdown
[](http://passportjs.org/) strategy for authenticating with Twitter API v2
```bash
npm install passport-twitter-2
```
```js
import { Strategy as TwitterStrategy } from 'passport-twitter-2';
passport.use(new TwitterStrategy({
clientID: TWITTER_CLIENT_ID,
appKey: TWITTER_CLIENT_SECRET,
callbackURL: "http://localhost:3000/auth/twitter/callback"
},
function(accessToken, profile, done) {
User.findOrCreate({ twitter: profile.id }, function (err, user) {
return done(err, user);
});
}
));
```
Use `passport.authenticate()`, specifying the `'twitter'` strategy, to
authenticate requests.
For example, as route middleware in an [Express](http://expressjs.com/)
application:
```js
app.get('/auth/twitter',
passport.authenticate('twitter'));
app.get('/auth/twitter/callback',
passport.authenticate('twitter', { failureRedirect: '/login' }),
function(req, res) {
// Successful authentication, redirect home.
res.redirect('/');
});
```
For Node.js 18+ you can skip installing node-fetch with
```bash
npm i passport-twitter-2 --omit=optional
```
and use built-in fetch instead.
- [Ēriks Remess](http://github.com/EriksRemess)
[](http://opensource.org/licenses/MIT)
Copyright (c) 2022 Ēriks Remess