revert passport.js to original hackathon starter implementation
This commit is contained in:
@ -23,29 +23,14 @@ passport.serializeUser(function(user, done) {
|
|||||||
});
|
});
|
||||||
|
|
||||||
passport.deserializeUser(function(id, done) {
|
passport.deserializeUser(function(id, done) {
|
||||||
User.findById(id, function(err, user) {
|
User.findOne({
|
||||||
if (err) { return done(err); }
|
_id: id
|
||||||
done(null, user);
|
}, '-password', function(err, user) {
|
||||||
|
console.log(user);
|
||||||
|
done(err, user);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
//function sendWelcomeEmail(u) {
|
|
||||||
// var transporter = nodemailer.createTransport({
|
|
||||||
// service: 'Mandrill',
|
|
||||||
// auth: {
|
|
||||||
// user: secrets.mandrill.user,
|
|
||||||
// pass: secrets.mandrill.password
|
|
||||||
// }
|
|
||||||
// });
|
|
||||||
// var mailOptions = {
|
|
||||||
// to: u.email,
|
|
||||||
// from: 'Team@freecodecamp.com',
|
|
||||||
// subject: 'Welcome to Free Code Camp ' + u.name + '!',
|
|
||||||
// text: 'Hello,\n\n' +
|
|
||||||
// 'Welcome to Free Code Camp!'
|
|
||||||
// };
|
|
||||||
//}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* OAuth Strategy Overview
|
* OAuth Strategy Overview
|
||||||
*
|
*
|
||||||
@ -62,45 +47,23 @@ passport.deserializeUser(function(id, done) {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
// Sign in with Twitter.
|
// Sign in with Twitter.
|
||||||
passport.use(
|
|
||||||
new TwitterStrategy(
|
passport.use(new TwitterStrategy(secrets.twitter, function(req, accessToken, tokenSecret, profile, done) {
|
||||||
secrets.twitter, function(req, accessToken, tokenSecret, profile, done) {
|
|
||||||
if (req.user) {
|
if (req.user) {
|
||||||
User.findOne({ twitter: profile.id }, function(err, existingUser) {
|
User.findOne({ twitter: profile.id }, function(err, existingUser) {
|
||||||
if (err) { return done(err); }
|
|
||||||
|
|
||||||
if (existingUser) {
|
if (existingUser) {
|
||||||
req.flash('errors', {
|
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.' });
|
||||||
msg: [
|
done(err);
|
||||||
'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.'
|
|
||||||
].join('')
|
|
||||||
});
|
|
||||||
done();
|
|
||||||
} else {
|
} else {
|
||||||
User.findById(req.user.id, function(err, user) {
|
User.findById(req.user.id, function(err, user) {
|
||||||
if (err) { return done(err); }
|
|
||||||
|
|
||||||
user.twitter = profile.id;
|
user.twitter = profile.id;
|
||||||
user.tokens.push({
|
user.tokens.push({ kind: 'twitter', accessToken: accessToken, tokenSecret: tokenSecret });
|
||||||
kind: 'twitter',
|
|
||||||
accessToken: accessToken,
|
|
||||||
tokenSecret: tokenSecret
|
|
||||||
});
|
|
||||||
user.profile.name = user.profile.name || profile.displayName;
|
user.profile.name = user.profile.name || profile.displayName;
|
||||||
user.profile.username = user.profile.username || profile.username;
|
user.profile.location = user.profile.location || profile._json.location;
|
||||||
|
user.profile.picture = user.profile.picture || profile._json.profile_image_url_https;
|
||||||
user.profile.location =
|
|
||||||
user.profile.location || profile._json.location;
|
|
||||||
|
|
||||||
user.profile.picture =
|
|
||||||
user.profile.picture || profile._json.profile_image_url_https;
|
|
||||||
|
|
||||||
user.profile.username = profile.username;
|
|
||||||
user.save(function(err) {
|
user.save(function(err) {
|
||||||
req.flash('info', { msg: 'Twitter account has been linked.' });
|
req.flash('info', { msg: 'Twitter account has been linked.' });
|
||||||
done(null, user);
|
done(err, user);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@ -108,121 +71,210 @@ passport.use(
|
|||||||
|
|
||||||
} else {
|
} else {
|
||||||
User.findOne({ twitter: profile.id }, function(err, existingUser) {
|
User.findOne({ twitter: profile.id }, function(err, existingUser) {
|
||||||
if (err) { return done(err); }
|
if (existingUser) return done(null, existingUser);
|
||||||
if (!existingUser || (existingUser && existingUser.twitter == profile.id)) {
|
var user = new User();
|
||||||
var user = existingUser || new User();
|
// Twitter will not provide an email address. Period.
|
||||||
|
// But a person’s twitter username is guaranteed to be unique
|
||||||
|
// so we can "fake" a twitter email address as follows:
|
||||||
|
user.email = profile.username + "@please_add_your_email_here.com";
|
||||||
|
user.profile.username = profile.username;
|
||||||
user.twitter = profile.id;
|
user.twitter = profile.id;
|
||||||
user.email = user.email || '';
|
user.tokens.push({ kind: 'twitter', accessToken: accessToken, tokenSecret: tokenSecret });
|
||||||
user.tokens.push({
|
user.profile.name = profile.displayName;
|
||||||
kind: 'twitter',
|
user.profile.location = profile._json.location;
|
||||||
accessToken: accessToken,
|
user.profile.picture = profile._json.profile_image_url_https;
|
||||||
tokenSecret: tokenSecret
|
user.save(function(err) {
|
||||||
|
done(err, user);
|
||||||
});
|
});
|
||||||
user.profile.name = user.profile.name || profile.displayName;
|
|
||||||
user.profile.username = user.profile.username || profile.username;
|
|
||||||
|
|
||||||
user.profile.location =
|
|
||||||
user.profile.location || profile._json.location;
|
|
||||||
user.profile.picture =
|
|
||||||
user.profile.picture || profile._json.profile_image_url_https;
|
|
||||||
|
|
||||||
user.save(function (err) {
|
|
||||||
if (err) {
|
|
||||||
return done(err);
|
|
||||||
}
|
|
||||||
done(null, user);
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
return done("Sorry, we experienced an error. This has been reported. Try logging in with a different authentication method.");
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
})
|
}));
|
||||||
);
|
|
||||||
|
|
||||||
// Sign in with LinkedIn.
|
// Sign in with Google.
|
||||||
passport.use(
|
|
||||||
new LinkedInStrategy(
|
passport.use(new GoogleStrategy(secrets.google, function(req, accessToken, refreshToken, profile, done) {
|
||||||
secrets.linkedin, function(req, accessToken, refreshToken, profile, done) {
|
|
||||||
if (req.user) {
|
if (req.user) {
|
||||||
User.findOne({ linkedin: profile.id }, function(err, existingUser) {
|
User.findOne({ google: profile.id }, function(err, existingUser) {
|
||||||
if (err) { return done(err); }
|
|
||||||
|
|
||||||
if (existingUser) {
|
if (existingUser) {
|
||||||
req.flash('errors', {
|
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.' });
|
||||||
msg: [
|
done(err);
|
||||||
'There is already a LinkedIn account that belongs to you.',
|
|
||||||
'Sign in with that account or delete it,',
|
|
||||||
'then link it with your current account.'
|
|
||||||
].join('')
|
|
||||||
});
|
|
||||||
done();
|
|
||||||
} else {
|
} else {
|
||||||
User.findById(req.user.id, function(err, user) {
|
User.findById(req.user.id, function(err, user) {
|
||||||
if (err) { return done(err); }
|
user.google = profile.id;
|
||||||
|
user.tokens.push({ kind: 'google', accessToken: accessToken });
|
||||||
user.linkedin = profile.id;
|
|
||||||
user.tokens.push({
|
|
||||||
kind: 'linkedin',
|
|
||||||
accessToken: accessToken
|
|
||||||
});
|
|
||||||
user.profile.name = user.profile.name || profile.displayName;
|
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.' });
|
||||||
|
done(err, user);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
User.findOne({ google: 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 Google manually from Account Settings.' });
|
||||||
|
done(err);
|
||||||
|
} 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);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}));
|
||||||
|
|
||||||
user.profile.location =
|
// Sign in with LinkedIn.
|
||||||
user.profile.location || profile._json.location.name;
|
|
||||||
|
|
||||||
user.profile.picture =
|
passport.use(new LinkedInStrategy(secrets.linkedin, function(req, accessToken, refreshToken, profile, done) {
|
||||||
user.profile.picture || profile._json.pictureUrl;
|
if (req.user) {
|
||||||
|
User.findOne({ linkedin: profile.id }, function(err, existingUser) {
|
||||||
user.profile.website =
|
if (existingUser) {
|
||||||
user.profile.website || profile._json.publicProfileUrl;
|
req.flash('errors', { msg: 'There is already a LinkedIn account that belongs to you. Sign in with that account or delete it, then link it with your current account.' });
|
||||||
|
done(err);
|
||||||
user.save(function(err, _user) {
|
} else {
|
||||||
if (err) { return done(err); }
|
User.findById(req.user.id, function(err, user) {
|
||||||
req.flash(
|
user.linkedin = profile.id;
|
||||||
'info', { msg: 'LinkedIn account has been linked.' }
|
user.tokens.push({ kind: 'linkedin', accessToken: accessToken });
|
||||||
);
|
user.profile.name = user.profile.name || profile.displayName;
|
||||||
done(null, user);
|
user.profile.location = user.profile.location || profile._json.location.name;
|
||||||
|
user.profile.picture = user.profile.picture || profile._json.pictureUrl;
|
||||||
|
user.profile.website = user.profile.website || profile._json.publicProfileUrl;
|
||||||
|
user.save(function(err) {
|
||||||
|
req.flash('info', { msg: 'LinkedIn account has been linked.' });
|
||||||
|
done(err, user);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
User.findOne({ linkedin: profile.id }, function(err, existingUser) {
|
User.findOne({ linkedin: profile.id }, function(err, existingUser) {
|
||||||
if (err) { return done(err); }
|
if (existingUser) return done(null, existingUser);
|
||||||
|
User.findOne({ email: profile._json.emailAddress }, function(err, existingEmailUser) {
|
||||||
if (existingUser) { return done(null, existingUser); }
|
if (existingEmailUser) {
|
||||||
User.findOne(
|
req.flash('errors', { msg: 'There is already an account using this email address. Sign in to that account and link it with LinkedIn manually from Account Settings.' });
|
||||||
{ email: profile._json.emailAddress },
|
done(err);
|
||||||
function(err, existingEmailUser) {
|
} else {
|
||||||
if (err) { return done(err); }
|
var user = new User();
|
||||||
|
|
||||||
var user = existingEmailUser || new User();
|
|
||||||
|
|
||||||
user.linkedin = profile.id;
|
user.linkedin = profile.id;
|
||||||
user.tokens.push({
|
user.tokens.push({ kind: 'linkedin', accessToken: accessToken });
|
||||||
kind: 'linkedin',
|
user.email = profile._json.emailAddress;
|
||||||
accessToken: accessToken
|
user.profile.name = profile.displayName;
|
||||||
});
|
user.profile.location = profile._json.location.name;
|
||||||
user.email = user.email || profile._json.emailAddress;
|
user.profile.picture = profile._json.pictureUrl;
|
||||||
user.profile.name = user.profile.name || profile.displayName;
|
user.profile.website = profile._json.publicProfileUrl;
|
||||||
|
|
||||||
user.profile.location =
|
|
||||||
user.profile.location || profile._json.location.name;
|
|
||||||
|
|
||||||
user.profile.picture =
|
|
||||||
user.profile.picture || profile._json.pictureUrl;
|
|
||||||
|
|
||||||
user.profile.website =
|
|
||||||
user.profile.website || profile._json.publicProfileUrl;
|
|
||||||
|
|
||||||
user.challengesComplete = user.challengesCompleted || [];
|
|
||||||
user.save(function(err) {
|
user.save(function(err) {
|
||||||
if (err) { return done(err); }
|
done(err, user);
|
||||||
done(null, user);
|
});
|
||||||
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
}
|
||||||
|
}));
|
||||||
|
|
||||||
|
// Sign in with GitHub.
|
||||||
|
|
||||||
|
passport.use(new GitHubStrategy(secrets.github, function(req, accessToken, refreshToken, profile, done) {
|
||||||
|
if (req.user) {
|
||||||
|
User.findOne({ github: profile.id }, 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);
|
||||||
|
} 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.' });
|
||||||
|
done(err, user);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
} 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);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}));
|
||||||
|
|
||||||
|
// Sign in with Facebook.
|
||||||
|
|
||||||
|
passport.use(new FacebookStrategy(secrets.facebook, function(req, accessToken, refreshToken, profile, done) {
|
||||||
|
if (req.user) {
|
||||||
|
User.findOne({ facebook: profile.id }, function(err, existingUser) {
|
||||||
|
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);
|
||||||
|
} 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';
|
||||||
|
user.save(function(err) {
|
||||||
|
req.flash('info', { msg: 'Facebook account has been linked.' });
|
||||||
|
done(err, user);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
User.findOne({ facebook: 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 Facebook manually from Account Settings.' });
|
||||||
|
done(err);
|
||||||
|
} else {
|
||||||
|
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);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}));
|
}));
|
||||||
@ -418,88 +470,7 @@ passport.use(
|
|||||||
}
|
}
|
||||||
}));
|
}));
|
||||||
|
|
||||||
// Sign in with Google.
|
|
||||||
|
|
||||||
passport.use(
|
|
||||||
new GoogleStrategy(
|
|
||||||
secrets.google, function(req, accessToken, refreshToken, profile, done) {
|
|
||||||
if (req.user) {
|
|
||||||
User.findOne({ google: profile.id }, function(err, existingUser) {
|
|
||||||
if (err) { return done(err); }
|
|
||||||
|
|
||||||
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.'
|
|
||||||
].join(' ')
|
|
||||||
});
|
|
||||||
done();
|
|
||||||
} else {
|
|
||||||
User.findById(req.user.id, function(err, user) {
|
|
||||||
if (err) { return done(err); }
|
|
||||||
|
|
||||||
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) {
|
|
||||||
if (err) { return done(err); }
|
|
||||||
req.flash('info', { msg: 'Google account has been linked.' });
|
|
||||||
done(null, user);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
User.findOne({ google: profile.id }, function(err, existingUser) {
|
|
||||||
if (err) { return done(err); }
|
|
||||||
|
|
||||||
if (existingUser) { return done(null, existingUser); }
|
|
||||||
User.findOne(
|
|
||||||
{ email: profile._json.email }, function(err, existingEmailUser) {
|
|
||||||
if (err) { return done(err); }
|
|
||||||
|
|
||||||
var user = existingEmailUser || new User();
|
|
||||||
user.email = user.email || profile._json.email;
|
|
||||||
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;
|
|
||||||
|
|
||||||
//TODO: Greeting email
|
|
||||||
//if (!existingEmailUser) {
|
|
||||||
// sendWelcomeEmail(user);
|
|
||||||
//}
|
|
||||||
user.save(function(err) {
|
|
||||||
if (err) { return done(err); }
|
|
||||||
done(null, user);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}));
|
|
||||||
|
|
||||||
|
|
||||||
function isAuthenticated(req, res, next) {
|
function isAuthenticated(req, res, next) {
|
||||||
|
Reference in New Issue
Block a user