facebook integration

v1.18.x
Julian Lam 12 years ago
parent c113f4151b
commit 9c05a76fcb

@ -21,6 +21,10 @@ var config = {
"google": { "google": {
"id": '', "id": '',
"secret": '' "secret": ''
},
"facebook": {
"app_id": '',
"secret": ''
} }
} }

@ -22,6 +22,7 @@
"passport-local": "0.1.6", "passport-local": "0.1.6",
"passport-twitter": "0.1.4", "passport-twitter": "0.1.4",
"passport-google-oauth": "0.1.5", "passport-google-oauth": "0.1.5",
"passport-facebook": "0.1.5",
"less-middleware": "0.1.11" "less-middleware": "0.1.11"
}, },
"devDependencies": {}, "devDependencies": {},

@ -17,6 +17,7 @@
<ul class="alt-logins"> <ul class="alt-logins">
<li data-url="/auth/twitter" class="twitter {twitter:display}"></li> <li data-url="/auth/twitter" class="twitter {twitter:display}"></li>
<li data-url="/auth/google" class="google {google:display}"></li> <li data-url="/auth/google" class="google {google:display}"></li>
<li data-url="/auth/facebook" class="facebook {facebook:display}">Login via Facebook</li>
</ul> </ul>
</div> </div>
</div> </div>

@ -174,6 +174,31 @@ var config = require('../config.js'),
}); });
} }
User.loginViaFacebook = function(fbid, name, email, callback) {
User.get_uid_by_fbid(fbid, function(uid) {
if (uid !== null) {
// Existing User
callback(null, {
uid: uid
});
} else {
// New User
User.create(name, null, email, function(err, uid) {
if (err !== null) {
callback(err);
} else {
// Save twitter-specific information to the user
RDB.set('uid:' + uid + ':fbid', fbid);
RDB.set('fbid:' + fbid + ':uid', uid);
callback(null, {
uid: uid
});
}
});
}
});
}
User.logout = function(sessionID, callback) { User.logout = function(sessionID, callback) {
User.get_uid_by_session(sessionID, function(uid) { User.get_uid_by_session(sessionID, function(uid) {
if (uid) { if (uid) {
@ -257,6 +282,12 @@ var config = require('../config.js'),
}); });
} }
User.get_uid_by_fbid = function(fbid, callback) {
RDB.get('fbid:' + fbid + ':uid', function(uid) {
callback(uid);
});
}
User.session_ping = function(sessionID, uid) { User.session_ping = function(sessionID, uid) {
// Start, replace, or extend a session // Start, replace, or extend a session
RDB.get('sess:' + sessionID, function(session) { RDB.get('sess:' + sessionID, function(session) {

@ -10,6 +10,7 @@ var express = require('express'),
passportLocal = require('passport-local').Strategy, passportLocal = require('passport-local').Strategy,
passportTwitter = require('passport-twitter').Strategy, passportTwitter = require('passport-twitter').Strategy,
passportGoogle = require('passport-google-oauth').OAuth2Strategy, passportGoogle = require('passport-google-oauth').OAuth2Strategy,
passportFacebook = require('passport-facebook').Strategy,
login_strategies = []; login_strategies = [];
passport.use(new passportLocal(function(user, password, next) { passport.use(new passportLocal(function(user, password, next) {
@ -34,7 +35,7 @@ if (config.twitter && config.twitter.key && config.twitter.key.length > 0 && con
login_strategies.push('twitter'); login_strategies.push('twitter');
} }
if (config.google.id.length > 0 && config.google.secret.length > 0) { if (config.google && config.google.id.length > 0 && config.google.secret.length > 0) {
passport.use(new passportGoogle({ passport.use(new passportGoogle({
clientID: config.google.id, clientID: config.google.id,
clientSecret: config.google.secret, clientSecret: config.google.secret,
@ -44,11 +45,26 @@ if (config.google.id.length > 0 && config.google.secret.length > 0) {
if (err) { return done(err); } if (err) { return done(err); }
done(null, user); done(null, user);
}); });
})) }));
login_strategies.push('google'); login_strategies.push('google');
} }
if (config.facebook && config.facebook.app_id.length > 0 && config.facebook.secret.length > 0) {
passport.use(new passportFacebook({
clientID: config.facebook.app_id,
clientSecret: config.facebook.secret,
callbackURL: config.url + 'auth/facebook/callback'
}, function(accessToken, refreshToken, profile, done) {
global.modules.user.loginViaFacebook(profile.id, profile.displayName, profile.emails[0].value, function(err, user) {
if (err) { return done(err); }
done(null, user);
});
}));
login_strategies.push('facebook');
}
passport.serializeUser(function(user, done) { passport.serializeUser(function(user, done) {
done(null, user.uid); done(null, user.uid);
}); });
@ -207,6 +223,15 @@ passport.deserializeUser(function(uid, done) {
})); }));
} }
if (login_strategies.indexOf('facebook') !== -1) {
app.get('/auth/facebook', passport.authenticate('facebook', { scope: 'email' }));
app.get('/auth/facebook/callback', passport.authenticate('facebook', {
successRedirect: '/',
failureRedirect: '/login'
}));
}
app.get('/reset/:code', function(req, res) { app.get('/reset/:code', function(req, res) {
res.send(templates['header'] + templates['reset_code'].parse({ reset_code: req.params.code }) + templates['footer']); res.send(templates['header'] + templates['reset_code'].parse({ reset_code: req.params.code }) + templates['footer']);
}); });

Loading…
Cancel
Save