Permalink
Browse files

Adding example application implementation and notes

  • Loading branch information...
1 parent 25489a4 commit d6120aa27173949f18c98894facc76c3f9843ad0 @kulor committed Aug 26, 2012
Showing with 68 additions and 1 deletion.
  1. +1 −0 .gitignore
  2. +6 −1 README.md
  3. +51 −0 app.js
  4. +10 −0 package.json
View
@@ -0,0 +1 @@
+node_modules
View
@@ -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
View
51 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'));
View
@@ -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.