Created
August 3, 2012 07:32
-
-
Save shekharsuman1/3245417 to your computer and use it in GitHub Desktop.
Authenticating with Passport - Google, Yahoo, Facebook, Twitter.
This file contains 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
<p> | |
<a href="/">Home</a> | | |
<a href="/account">Account</a> | | |
<a href="/logout">Log Out</a> | |
</p> | |
<p>ID: <%= user.id %></p> | |
<p>Username: <%= user.username %></p> | |
<p>Name: <%= user.displayName %></p> | |
<h4><%= JSON.stringify(user) %></h4> <!-- This line will output all the data associated with the Logged In user returned by Passport --> |
This file contains 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') | |
, util = require('util') | |
, GoogleStrategy = require('passport-google').Strategy | |
, FacebookStrategy = require('passport-facebook').Strategy | |
, TwitterStrategy = require('passport-twitter').Strategy | |
, YahooStrategy = require('passport-yahoo').Strategy; | |
var FACEBOOK_APP_ID = " SECRET " | |
var FACEBOOK_APP_SECRET = " SECRET "; | |
var TWITTER_CONSUMER_KEY = " SECRET "; | |
var TWITTER_CONSUMER_SECRET = " SECRET "; | |
passport.serializeUser(function(user, done) { | |
done(null, user); | |
}); | |
passport.deserializeUser(function(obj, done) { | |
done(null, obj); | |
}); | |
passport.use(new FacebookStrategy({ | |
clientID: FACEBOOK_APP_ID, | |
clientSecret: FACEBOOK_APP_SECRET, | |
callbackURL: "http://authall.jit.su:80/auth/facebook/callback" | |
}, | |
function(accessToken, refreshToken, profile, done) { | |
// asynchronous verification, for effect... | |
process.nextTick(function () { | |
return done(null, profile); | |
}); | |
} | |
)); | |
passport.use(new GoogleStrategy({ | |
returnURL: 'http://authall.jit.su:80/auth/google/return', | |
realm: 'http://authall.jit.su:80/' | |
}, | |
function(identifier, profile, done) { | |
// asynchronous verification, for effect... | |
process.nextTick(function () { | |
profile.identifier = identifier; | |
return done(null, profile); | |
}); | |
} | |
)); | |
passport.use(new TwitterStrategy({ | |
consumerKey: TWITTER_CONSUMER_KEY, | |
consumerSecret: TWITTER_CONSUMER_SECRET, | |
callbackURL: "http://authall.jit.su:80/auth/twitter/callback" | |
}, | |
function(token, tokenSecret, profile, done) { | |
// asynchronous verification, for effect... | |
process.nextTick(function () { | |
return done(null, profile); | |
}); | |
} | |
)); | |
passport.use(new YahooStrategy({ | |
returnURL: 'http://authall.jit.su:80/auth/yahoo/return', | |
realm: 'http://authall.jit.su:80/' | |
}, | |
function(identifier, profile, done) { | |
// asynchronous verification, for effect... | |
process.nextTick(function () { | |
profile.identifier = identifier; | |
return done(null, profile); | |
}); | |
} | |
)); | |
var app = express.createServer(); | |
// configure Express | |
app.configure(function() { | |
app.set('views', __dirname + '/views'); | |
app.set('view engine', 'ejs'); | |
app.use(express.logger()); | |
app.use(express.cookieParser()); | |
app.use(express.bodyParser()); | |
app.use(express.methodOverride()); | |
app.use(express.session({ secret: 'keyboard cat' })); | |
app.use(passport.initialize()); | |
app.use(passport.session()); | |
app.use(app.router); | |
app.use(express.static(__dirname + '/public')); | |
}); | |
app.get('/', function(req, res){ | |
res.render('index', { user: req.user }); | |
}); | |
app.get('/account', ensureAuthenticated, function(req, res){ | |
res.render('account', { user: req.user }); | |
console.log(req.user); | |
}); | |
app.get('/login', function(req, res){ | |
res.render('login', { user: req.user }); | |
}); | |
app.get('/auth/facebook', | |
passport.authenticate('facebook'), | |
function(req, res){ | |
}); | |
app.get('/auth/facebook/callback', | |
passport.authenticate('facebook', { failureRedirect: '/login' }), | |
function(req, res) { | |
res.redirect('/'); | |
}); | |
app.get('/auth/google', | |
passport.authenticate('google', { failureRedirect: '/login' }), | |
function(req, res) { | |
res.redirect('/'); | |
}); | |
app.get('/auth/google/return', | |
passport.authenticate('google', { failureRedirect: '/login' }), | |
function(req, res) { | |
res.redirect('/'); | |
}); | |
app.get('/auth/twitter', | |
passport.authenticate('twitter'), | |
function(req, res){ | |
}); | |
app.get('/auth/twitter/callback', | |
passport.authenticate('twitter', { failureRedirect: '/login' }), | |
function(req, res) { | |
res.redirect('/'); | |
}); | |
//YAHOO | |
app.get('/auth/yahoo', | |
passport.authenticate('yahoo', { failureRedirect: '/login' }), | |
function(req, res) { | |
res.redirect('/'); | |
}); | |
app.get('/auth/yahoo/return', | |
passport.authenticate('yahoo', { failureRedirect: '/login' }), | |
function(req, res) { | |
res.redirect('/'); | |
}); | |
//LOGOUT | |
app.get('/logout', function(req, res){ | |
req.logout(); | |
res.redirect('/'); | |
}); | |
app.listen(80); | |
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