Skip to content

Commit

Permalink
Adding example application implementation and notes
Browse files Browse the repository at this point in the history
  • Loading branch information
kulor committed Aug 26, 2012
1 parent 25489a4 commit d6120aa
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
@@ -0,0 +1 @@
node_modules
7 changes: 6 additions & 1 deletion README.md
@@ -1,4 +1,9 @@
passportjs-express-example
==========================

Passportjs basic working example
Passportjs basic working example as I felt the docs were too scattered to know how to hit the ground running.

Running the app with your credentials
-------------------------------------

TWITTER_CONSUMER_KEY=xxx TWITTER_CONSUMER_SECRET=xxx node app.js
51 changes: 51 additions & 0 deletions app.js
@@ -0,0 +1,51 @@
var express = require('express'),
app = express(),
passport = require('passport'),
TwitterStrategy = require('passport-twitter').Strategy;

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.set('hostname', 'localhost');
app.set('port', 4000);
app.set('TWITTER_CONSUMER_KEY', process.env.TWITTER_CONSUMER_KEY);
app.set('TWITTER_CONSUMER_SECRET', process.env.TWITTER_CONSUMER_SECRET);
});

passport.use(new TwitterStrategy({
consumerKey: app.get('TWITTER_CONSUMER_KEY'),
consumerSecret: app.get('TWITTER_CONSUMER_SECRET'),
callbackURL: 'http://' + app.get('hostname') + ':' + app.get('port') + '/auth/twitter/callback'
},
function(token, tokenSecret, profile, done) {
done(null, profile);
}
));

passport.serializeUser(function(user, done) {
done(null, user);
});

passport.deserializeUser(function(user, done) {
done(null, user);
});

app.get('/', function(req, res){
if(req.user){
res.send(req.user);
} else {
res.send('<a href="/auth/twitter">Sign in with Twitter</a>');
}
});
app.get('/auth/twitter', passport.authenticate('twitter'));
app.get('/auth/twitter/callback',
passport.authenticate('twitter', { successRedirect: '/',
failureRedirect: '/login' }));

console.log('App running, head to http://' + app.get('hostname') + ':' + app.get('port') + ' to sign in with Twitter');
app.listen(app.get('port'));
10 changes: 10 additions & 0 deletions package.json
@@ -0,0 +1,10 @@
{
"name": "passportjs-express-example",
"version": "0.0.1",
"private": false,
"dependencies": {
"express": ">=3.0.0rc3",
"passport": "*",
"passport-twitter": "*"
}
}

0 comments on commit d6120aa

Please sign in to comment.