freeCodeCamp/config/passport.js

314 lines
12 KiB
JavaScript
Raw Normal View History

var _ = require('underscore');
var passport = require('passport');
var LocalStrategy = require('passport-local').Strategy;
var FacebookStrategy = require('passport-facebook').Strategy;
var TwitterStrategy = require('passport-twitter').Strategy;
var GitHubStrategy = require('passport-github').Strategy;
var GoogleStrategy = require('passport-google-oauth').OAuth2Strategy;
var OAuthStrategy = require('passport-oauth').OAuthStrategy; // Tumblr
var OAuth2Strategy = require('passport-oauth').OAuth2Strategy; // Venmo, Foursquare
var User = require('../models/User');
var secrets = require('./secrets');
2013-11-19 00:43:45 +00:00
passport.serializeUser(function(user, done) {
done(null, user.id);
});
passport.deserializeUser(function(id, done) {
User.findById(id, function (err, user) {
done(err, user);
});
});
/**
* Sign in using Email and Password.
*/
passport.use(new LocalStrategy({ usernameField: 'email' }, function(email, password, done) {
User.findOne({ email: email }, function(err, user) {
2014-01-24 03:47:21 +00:00
if (!user) return done(null, false, { message: 'Email ' + email + ' not found'});
user.comparePassword(password, function(err, isMatch) {
2014-01-24 03:47:21 +00:00
if (isMatch) {
return done(null, user);
} else {
2014-01-12 20:31:17 +00:00
return done(null, false, { message: 'Invalid email or password.' });
}
});
});
}));
2014-01-31 21:28:12 +00:00
/**
* Sign in with Facebook.
*/
2014-01-31 21:28:12 +00:00
2014-02-05 16:37:48 +00:00
passport.use(new FacebookStrategy(secrets.facebook, function(req, accessToken, refreshToken, profile, done) {
if (req.user) {
User.findOne({ $or: [{ facebook: profile.id }, { email: profile.email }] }, function(err, existingUser) {
2014-01-31 21:28:12 +00:00
if (existingUser) {
req.flash('errors', { msg: 'There is already a Facebook account that belongs to you. Sign in with that account or delete it, then link it with your current account.' });
done(err);
2014-01-31 21:28:12 +00:00
} else {
User.findById(req.user.id, function(err, user) {
user.facebook = profile.id;
user.tokens.push({ kind: 'facebook', accessToken: accessToken });
user.profile.name = user.profile.name || profile.displayName;
user.profile.gender = user.profile.gender || profile._json.gender;
user.profile.picture = user.profile.picture || 'https://graph.facebook.com/' + profile.id + '/picture?type=large';
2014-01-31 21:28:12 +00:00
user.save(function(err) {
req.flash('info', { msg: 'Facebook account has been linked.' });
2014-01-31 21:28:12 +00:00
done(err, user);
});
});
}
2013-12-03 00:16:27 +00:00
});
} else {
User.findOne({ facebook: profile.id }, function(err, existingUser) {
console.log(profile)
if (existingUser) return done(null, existingUser);
var user = new User();
user.email = profile._json.email;
user.facebook = profile.id;
user.tokens.push({ kind: 'facebook', accessToken: accessToken });
user.profile.name = profile.displayName;
user.profile.gender = profile._json.gender;
user.profile.picture = 'https://graph.facebook.com/' + profile.id + '/picture?type=large';
user.profile.location = (profile._json.location) ? profile._json.location.name : '';
user.save(function(err) {
done(err, user);
});
});
}
}));
2013-12-06 06:44:45 +00:00
2014-02-01 03:39:13 +00:00
/**
* Sign in with GitHub.
*/
passport.use(new GitHubStrategy(secrets.github, function(req, accessToken, refreshToken, profile, done) {
if (req.user) {
2014-02-01 03:39:13 +00:00
User.findOne({ $or: [{ github: profile.id }, { email: profile.email }] }, function(err, existingUser) {
if (existingUser) {
req.flash('errors', { msg: 'There is already a GitHub account that belongs to you. Sign in with that account or delete it, then link it with your current account.' });
done(err);
2014-02-01 03:39:13 +00:00
} else {
User.findById(req.user.id, function(err, user) {
user.github = profile.id;
user.tokens.push({ kind: 'github', accessToken: accessToken });
user.profile.name = user.profile.name || profile.displayName;
user.profile.picture = user.profile.picture || profile._json.avatar_url;
user.profile.location = user.profile.location || profile._json.location;
user.profile.website = user.profile.website || profile._json.blog;
user.save(function(err) {
req.flash('info', { msg: 'GitHub account has been linked.' });
2014-02-01 03:39:13 +00:00
done(err, user);
});
});
}
2013-12-03 22:38:14 +00:00
});
} else {
User.findOne({ github: profile.id }, function(err, existingUser) {
if (existingUser) return done(null, existingUser);
User.findOne({ email: profile._json.email }, function(err, existingEmailUser) {
if (existingEmailUser) {
req.flash('errors', { msg: 'There is already an account using this email address. Sign in to that account and link it with GitHub manually from Account Settings.' });
done(err);
} else {
var user = new User();
user.email = profile._json.email;
user.github = profile.id;
user.tokens.push({ kind: 'github', accessToken: accessToken });
user.profile.name = profile.displayName;
user.profile.picture = profile._json.avatar_url;
user.profile.location = profile._json.location;
user.profile.website = profile._json.blog;
user.save(function(err) {
done(err, user);
});
}
});
});
}
}));
2013-12-06 06:24:12 +00:00
2014-02-01 04:17:58 +00:00
/**
* Sign in with Twitter.
*/
passport.use(new TwitterStrategy(secrets.twitter, function(req, accessToken, tokenSecret, profile, done) {
if (req.user) {
User.findOne({ twitter: profile.id }, function(err, existingUser) {
2014-02-01 04:17:58 +00:00
if (existingUser) {
req.flash('errors', { msg: 'There is already a Twitter account that belongs to you. Sign in with that account or delete it, then link it with your current account.' });
done(err);
2014-02-01 04:17:58 +00:00
} else {
User.findById(req.user.id, function(err, user) {
user.twitter = profile.id;
user.tokens.push({ kind: 'twitter', accessToken: accessToken, tokenSecret: tokenSecret });
user.profile.name = user.profile.name || profile.displayName;
user.profile.location = user.profile.location || profile._json.location;
user.profile.picture = user.profile.picture || profile._json.profile_image_url;
user.save(function(err) {
req.flash('info', { msg: 'Twitter account has been linked.' });
2014-02-01 04:17:58 +00:00
done(err, user);
});
});
}
});
2014-02-01 04:17:58 +00:00
} else {
User.findOne({ twitter: profile.id }, function(err, existingUser) {
if (existingUser) return done(null, existingUser);
var user = new User();
// Twitter will not provide an email address. Period.
// But a persons twitter username is guaranteed to be unique
// so we can "fake" a twitter email address as follows:
user.email = profile.username + "@twitter.com";
user.twitter = profile.id;
user.tokens.push({ kind: 'twitter', accessToken: accessToken, tokenSecret: tokenSecret });
user.profile.name = profile.displayName;
user.profile.location = profile._json.location;
user.profile.picture = profile._json.profile_image_url;
user.save(function(err) {
done(err, user);
});
});
}
}));
2014-02-01 03:39:13 +00:00
/**
* Sign in with Google.
*/
passport.use(new GoogleStrategy(secrets.google, function(req, accessToken, refreshToken, profile, done) {
if (req.user) {
2014-02-01 03:39:13 +00:00
User.findOne({ $or: [{ google: profile.id }, { email: profile.email }] }, function(err, existingUser) {
if (existingUser) {
req.flash('errors', { msg: 'There is already a Google account that belongs to you. Sign in with that account or delete it, then link it with your current account.' });
done(err);
2014-02-01 03:39:13 +00:00
} else {
User.findById(req.user.id, function(err, user) {
user.google = profile.id;
user.tokens.push({ kind: 'google', accessToken: accessToken });
user.profile.name = user.profile.name || profile.displayName;
user.profile.gender = user.profile.gender || profile._json.gender;
user.profile.picture = user.profile.picture || profile._json.picture;
user.save(function(err) {
req.flash('info', { msg: 'Google account has been linked.' });
2014-02-01 03:39:13 +00:00
done(err, user);
});
});
}
2013-11-21 19:20:38 +00:00
});
} else {
User.findOne({ google: profile.id }, function(err, existingUser) {
if (existingUser) return done(null, existingUser);
User.findOne({ email: profile._json.email }, function(err2, existingEmailUser) {
if(existingEmailUser) {
req.flash('errors', { msg: 'There is already an account using this Google account\'s email address. Sign in with that account or delete it, then link it with your current account.' });
done(err2);
} else {
var user = new User();
user.email = profile._json.email;
user.google = profile.id;
user.tokens.push({ kind: 'google', accessToken: accessToken });
user.profile.name = profile.displayName;
user.profile.gender = profile._json.gender;
user.profile.picture = profile._json.picture;
user.save(function(err) {
done(err, user);
});
}
});
});
}
}));
2013-11-19 00:43:45 +00:00
/**
* Tumblr API
* Uses OAuth 1.0a Strategy.
*/
2013-12-07 03:49:40 +00:00
passport.use('tumblr', new OAuthStrategy({
requestTokenURL: 'http://www.tumblr.com/oauth/request_token',
accessTokenURL: 'http://www.tumblr.com/oauth/access_token',
userAuthorizationURL: 'http://www.tumblr.com/oauth/authorize',
consumerKey: secrets.tumblr.consumerKey,
consumerSecret: secrets.tumblr.consumerSecret,
callbackURL: secrets.tumblr.callbackURL,
passReqToCallback: true
2013-12-07 03:49:40 +00:00
},
function (req, token, tokenSecret, profile, done) {
User.findById(req.user._id, function(err, user) {
user.tokens.push({ kind: 'tumblr', accessToken: token, tokenSecret: tokenSecret });
user.save(function(err) {
2013-12-07 07:18:26 +00:00
done(err, user);
});
2013-12-07 03:49:40 +00:00
});
}
2013-12-07 03:49:40 +00:00
));
/**
* Foursquare API
* Uses OAuth 2.0 Strategy.
*/
passport.use('foursquare', new OAuth2Strategy({
authorizationURL: 'https://foursquare.com/oauth2/authorize',
tokenURL: 'https://foursquare.com/oauth2/access_token',
clientID: secrets.foursquare.clientId,
clientSecret: secrets.foursquare.clientSecret,
callbackURL: secrets.foursquare.redirectUrl,
passReqToCallback: true
},
function (req, accessToken, refreshToken, profile, done) {
User.findById(req.user._id, function(err, user) {
user.tokens.push({ kind: 'foursquare', accessToken: accessToken });
user.save(function(err) {
done(err, user);
});
});
}
));
/**
* Venmo API
* Uses OAuth 2.0 Strategy.
*/
2014-02-11 00:20:45 +00:00
passport.use('venmo', new OAuth2Strategy({
authorizationURL: 'https://api.venmo.com/v1/oauth/authorize',
tokenURL: 'https://api.venmo.com/v1/oauth/access_token',
clientID: secrets.venmo.clientId,
clientSecret: secrets.venmo.clientSecret,
callbackURL: secrets.venmo.redirectUrl,
passReqToCallback: true
},
function (req, accessToken, refreshToken, profile, done) {
User.findById(req.user._id, function(err, user) {
user.tokens.push({ kind: 'venmo', accessToken: accessToken });
user.save(function(err) {
done(err, user);
});
});
}
));
/**
* Login Required middleware.
*/
exports.isAuthenticated = function(req, res, next) {
2013-12-07 03:49:40 +00:00
if (req.isAuthenticated()) return next();
res.redirect('/login');
2013-12-08 07:10:49 +00:00
};
/**
* Authorization Required middleware.
*/
exports.isAuthorized = function(req, res, next) {
var provider = req.path.split('/').slice(-1)[0];
if (_.findWhere(req.user.tokens, { kind: provider })) next();
else res.redirect('/auth/' + provider);
};