Created
February 21, 2012 12:53
-
-
Save sw360cab/1876377 to your computer and use it in GitHub Desktop.
passport twitter
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var express = require('express') | |
, passport = require('passport') | |
, TwitterStrategy = require('passport-twitter').Strategy; | |
var TWITTER_CONSUMER_KEY = **************; | |
var TWITTER_CONSUMER_SECRET = *************; | |
var CALLBACK_URI = **************; | |
var token; | |
var tokenSecret; | |
passport.use(new TwitterStrategy({ | |
consumerKey: TWITTER_CONSUMER_KEY, | |
consumerSecret: TWITTER_CONSUMER_SECRET, | |
callbackURL: CALLBACK_URI | |
}, | |
function(token, tokenSecret, profile, done) { | |
// this LOG is never reached -> KO | |
console.log('TWITTER ****** in config' + token + ' ' + tokenSecret ); | |
process.nextTick(function () { | |
return done(null, profile); | |
}); | |
} | |
)); | |
passport.serializeUser(function(user, done) { | |
done(null, user); | |
}); | |
passport.deserializeUser(function(obj, done) { | |
done(null, obj); | |
}); | |
var app = express.createServer(); | |
app.configure(function() { | |
app.use(express.cookieParser()); | |
app.use(express.bodyParser()); | |
app.use(express.session({secret: 'keyboard cat' })); | |
app.use(passport.initialize()); | |
app.use(passport.session()); | |
app.use(app.router); | |
app.use(express.static(__dirname + '/static')); | |
}); | |
app.get('/auth/twitter', passport.authenticate('twitter'), | |
function(req, res){ | |
// This LOG is NOT reached but it's OK | |
console.log('TWITTER ****** auth twitter'); | |
}); | |
app.get('/auth/twitter/callback', | |
function(req, res) { | |
// This LOG is reached -> OK | |
console.log('TWITTER ****** logged'); | |
res.sendfile(__dirname + '/tweet.html'); | |
}); | |
app.listen(3000); | |
app.get('/', function (req, res) { | |
res.redirect('/login') | |
}); | |
app.get('/index.html', function (req, res) { | |
res.redirect('/') | |
}); | |
/* redirect to twitter auth */ | |
app.get('/login', function (req, res) { | |
res.sendfile(__dirname + '/login.html'); | |
}); | |
app.get('/logout', function(req, res){ | |
req.logout(); | |
res.redirect('/'); | |
}); | |
// Simple route middleware to ensure user is authenticated. | |
// Use this route middleware on any resource that needs to be protected. If | |
// the request is authenticated (typically via a persistent login session), | |
// the request will proceed. Otherwise, the user will be redirected to the | |
// login page. | |
function ensureAuthenticated(req, res, next) { | |
if (req.isAuthenticated()) { return next(); } | |
res.redirect('/login') | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment