Skip to content

Instantly share code, notes, and snippets.

@Dunnzilla
Forked from jaredhanson/gist:3989193
Created November 9, 2013 07:51
Show Gist options
  • Save Dunnzilla/7382902 to your computer and use it in GitHub Desktop.
Save Dunnzilla/7382902 to your computer and use it in GitHub Desktop.
var express = require('express'),
passport = require('passport'),
TwitterStrategy = require('passport-twitter').Strategy,
ensureLoggedIn = require('connect-ensure-login').ensureLoggedIn,
app = express();
app.use(express.static(__dirname + '/public'));
app.use(express.cookieParser());
app.use(express.session({ secret: 'keyboard cat' }));
app.use(passport.initialize());
app.use(passport.session());
passport.serializeUser(function(user, done) {
done(null, user);
});
passport.deserializeUser(function(obj, done) {
done(null, obj);
});
var TWITTER_CONSUMER_KEY = "INSERT_KEY_HERE";
var TWITTER_CONSUMER_SECRET = "INSERT_SECRET_HERE";
passport.use(new TwitterStrategy({
consumerKey: TWITTER_CONSUMER_KEY,
consumerSecret: TWITTER_CONSUMER_SECRET,
callbackURL: "http://127.0.0.1:3000/auth/twitter/callback"
},
function(token, tokenSecret, profile, done) {
// NOTE: You'll probably want to associate the Twitter profile with a
// user record in your application's DB.
var user = profile;
return done(null, user);
}
));
app.get('/', function(req, res){
res.send('Hello World');
});
app.get('/account',
ensureLoggedIn('/login'),
function(req, res) {
res.send('Hello ' + req.user.username);
});
app.get('/login',
function(req, res) {
res.send('<html><body><a href="/auth/twitter">Sign in with Twitter</a></body></html>');
});
app.get('/logout',
function(req, res) {
req.logout();
res.redirect('/');
});
app.get('/auth/twitter', passport.authenticate('twitter'));
app.get('/auth/twitter/callback', passport.authenticate('twitter', { successReturnToOrRedirect: '/', failureRedirect: '/login' }));
var server = app.listen(3000);
console.log('Express server started on port %s', server.address().port);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment