freeCodeCamp/generator.js

1042 lines
46 KiB
JavaScript
Raw Normal View History

2014-04-13 02:54:30 +00:00
var _ = require('underscore');
var colors = require('colors');
2014-04-13 01:37:55 +00:00
var fs = require('fs');
var inquirer = require('inquirer');
var M = require('mstring');
2014-04-18 00:15:48 +00:00
var os = require('os');
colors.setTheme({
silly: 'rainbow',
input: 'grey',
verbose: 'cyan',
prompt: 'grey',
info: 'green',
data: 'grey',
2014-04-14 18:07:13 +00:00
help: 'white',
warn: 'yellow',
debug: 'blue',
error: 'red'
});
2014-04-13 01:37:55 +00:00
inquirer.prompt({
type: 'list',
name: 'category',
2014-04-13 20:39:25 +00:00
message: 'Hackathon Starter:',
2014-04-14 18:07:13 +00:00
choices: ['☂ Authentication', '☎ Email Service', '☱ Exit']
2014-04-13 01:37:55 +00:00
}, function(answer) {
2014-04-14 18:07:13 +00:00
if (answer.category.match('Email Service')) {
inquirer.prompt({
type: 'list',
name: 'email',
message: 'Choose Email Delivery Service:',
choices: ['SendGrid', 'Mailgun', 'Cancel']
}, function(answer) {
var index;
var contactControllerFile = 'controllers/contact.js';
var userControllerFile = 'controllers/user.js';
2014-04-18 00:15:48 +00:00
var contactController = fs.readFileSync(contactControllerFile).toString().split(os.EOL);
var userController = fs.readFileSync(userControllerFile).toString().split(os.EOL);
2014-04-14 18:07:13 +00:00
if (answer.email.match('SendGrid')) {
// Change SMPT Transport to SendGrid in controllers/contact.js
index = contactController.indexOf('var smtpTransport = nodemailer.createTransport(\'SMTP\', {');
contactController.splice(index + 1, 1, ' service: \'SendGrid\',');
contactController.splice(index + 3, 1, ' user: secrets.sendgrid.user,');
contactController.splice(index + 4, 1, ' pass: secrets.sendgrid.password');
2014-04-18 00:15:48 +00:00
fs.writeFileSync(contactControllerFile, contactController.join(os.EOL));
2014-04-14 18:07:13 +00:00
// Change SMPT Transport to SendGrid in controllers/user.js
index = userController.indexOf(' var smtpTransport = nodemailer.createTransport(\'SMTP\', {');
userController.splice(index + 1, 1, ' service: \'SendGrid\',');
userController.splice(index + 3, 1, ' user: secrets.sendgrid.user,');
userController.splice(index + 4, 1, ' pass: secrets.sendgrid.password');
index = userController.indexOf(' var smtpTransport = nodemailer.createTransport(\'SMTP\', {', 1);
userController.splice(index + 1, 1, ' service: \'SendGrid\',');
userController.splice(index + 3, 1, ' user: secrets.sendgrid.user,');
userController.splice(index + 4, 1, ' pass: secrets.sendgrid.password');
2014-04-18 00:15:48 +00:00
fs.writeFileSync(userControllerFile, userController.join(os.EOL));
2014-04-14 18:07:13 +00:00
console.log('✓ Email Delivery Service has been switched to'.info, 'SendGrid'.help);
}
if (answer.email.match('Mailgun')) {
// Change SMPT Transport to Mailgun in controllers/contact.js
index = contactController.indexOf('var smtpTransport = nodemailer.createTransport(\'SMTP\', {');
contactController.splice(index + 1, 1, ' service: \'Mailgun\',');
contactController.splice(index + 3, 1, ' user: secrets.mailgun.login,');
contactController.splice(index + 4, 1, ' pass: secrets.mailgun.password');
2014-04-18 00:15:48 +00:00
fs.writeFileSync(contactControllerFile, contactController.join(os.EOL));
2014-04-14 18:07:13 +00:00
// Change SMPT Transport to Mailgun in controllers/user.js
index = userController.indexOf(' var smtpTransport = nodemailer.createTransport(\'SMTP\', {');
userController.splice(index + 1, 1, ' service: \'Mailgun\',');
userController.splice(index + 3, 1, ' user: secrets.mailgun.login,');
userController.splice(index + 4, 1, ' pass: secrets.mailgun.password');
index = userController.indexOf(' var smtpTransport = nodemailer.createTransport(\'SMTP\', {', 1);
userController.splice(index + 1, 1, ' service: \'Mailgun\',');
userController.splice(index + 3, 1, ' user: secrets.mailgun.login,');
userController.splice(index + 4, 1, ' pass: secrets.mailgun.password');
2014-04-18 00:15:48 +00:00
fs.writeFileSync(userControllerFile, userController.join(os.EOL));
2014-04-14 18:07:13 +00:00
console.log('✓ Email Delivery Service has been switched to'.info, '@'.error + 'mail'.data + 'gun'.error);
}
});
}
if (answer.category.match('Authentication')) {
2014-04-13 01:37:55 +00:00
inquirer.prompt({
type: 'checkbox',
message: 'Select Authentication Providers',
2014-04-13 01:37:55 +00:00
name: 'auth',
choices: [
new inquirer.Separator(M(function() {
/***
2014-04-14 18:18:21 +00:00
THIS TOOL IS STILL IN EXPERIMENTAL STAGE! USE AT YOUR OWN RISK.
ALWAYS USE VERSION CONTROL SYSTEM SO YOU COULD REVERT THE CHANGES.
REPORT BUGS AT HTTPS://GITHUB.COM/SAHAT/HACKATHON-STARTER/ISSUES/NEW ║
***/
})),
2014-04-13 01:37:55 +00:00
{ name: 'Facebook', checked: true },
{ name: 'GitHub', checked: true },
{ name: 'Google', checked: true },
{ name: 'Twitter', checked: true },
{ name: 'LinkedIn', checked: true },
{ name: 'Instagram', checked: true },
2014-04-14 18:11:28 +00:00
new inquirer.Separator('Press ctrl+c to cancel'.warn)
]
2014-04-13 01:37:55 +00:00
}, function(answer) {
2014-04-13 19:23:10 +00:00
var index;
var passportConfigFile = 'config/passport.js';
var userModelFile = 'models/User.js';
2014-04-14 06:30:28 +00:00
var appFile = 'app.js';
var secretsFile = 'config/secrets.js';
2014-04-13 19:23:10 +00:00
var profileTemplateFile = 'views/account/profile.jade';
var loginTemplateFile = 'views/account/login.jade';
2014-04-18 00:15:48 +00:00
var passportConfig = fs.readFileSync(passportConfigFile).toString().split(os.EOL);
var loginTemplate = fs.readFileSync(loginTemplateFile).toString().split(os.EOL);
var profileTemplate = fs.readFileSync(profileTemplateFile).toString().split(os.EOL);
var userModel = fs.readFileSync(userModelFile).toString().split(os.EOL);
var app = fs.readFileSync(appFile).toString().split(os.EOL);
var secrets = fs.readFileSync(secretsFile).toString().split(os.EOL);
2014-04-13 19:23:10 +00:00
/////////////////////////////
// Facebook Authentication //
/////////////////////////////
var facebookRoutes = M(function() {
/***
app.get('/auth/facebook', passport.authenticate('facebook', { scope: ['email', 'user_location'] }));
app.get('/auth/facebook/callback', passport.authenticate('facebook', { failureRedirect: '/login' }), function(req, res) {
res.redirect(req.session.returnTo || '/');
});
***/
});
var facebookStrategyRequire = "var FacebookStrategy = require('passport-facebook').Strategy;";
var facebookStrategy = M(function() {
/***
// Sign in with Facebook.
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) {
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);
});
}
});
});
}
}));
***/
});
var facebookButton = M(function() {
/***
a.btn.btn-block.btn-facebook.btn-social(href='/auth/facebook')
i.fa.fa-facebook
| Sign in with Facebook
***/
});
var facebookLinkUnlink = M(function() {
/***
if user.facebook
p: a.text-danger(href='/account/unlink/facebook') Unlink your Facebook account
else
p: a(href='/auth/facebook') Link your Facebook account
***/
});
var facebookModel = ' facebook: String,';
2014-04-13 02:54:30 +00:00
if (_.contains(answer.auth, 'Facebook')) {
if (passportConfig.indexOf(facebookStrategyRequire) < 0) {
2014-04-13 04:48:24 +00:00
// Add Facebook to config/passport.js
index = passportConfig.indexOf("var passport = require('passport');");
passportConfig.splice(index + 1, 0, facebookStrategyRequire);
index = passportConfig.indexOf('passport.deserializeUser(function(id, done) {');
passportConfig.splice(index + 6, 0, facebookStrategy);
2014-04-18 00:15:48 +00:00
fs.writeFileSync(passportConfigFile, passportConfig.join(os.EOL));
// Add Facebook to views/account/login.jade
loginTemplate.push(facebookButton);
2014-04-18 00:15:48 +00:00
fs.writeFileSync(loginTemplateFile, loginTemplate.join(os.EOL));
// Add Facebook to views/account/profile.jade
index = profileTemplate.indexOf(' h3 Linked Accounts');
profileTemplate.splice(index + 1, 0, facebookLinkUnlink);
2014-04-18 00:15:48 +00:00
fs.writeFileSync(profileTemplateFile, profileTemplate.join(os.EOL));
// Add Facebook to models/User.js
index = userModel.indexOf(' tokens: Array,');
2014-04-13 19:23:10 +00:00
userModel.splice(index - 1, 0, facebookModel);
2014-04-18 00:15:48 +00:00
fs.writeFileSync(userModelFile, userModel.join(os.EOL));
// Add Facebook to app.js
index = app.indexOf(' * OAuth routes for sign-in.');
app.splice(index + 3, 0, facebookRoutes);
2014-04-18 00:15:48 +00:00
fs.writeFileSync(appFile, app.join(os.EOL));
console.log('✓ Facebook authentication has been added.'.info);
2014-04-13 02:54:30 +00:00
} else {
console.log('✓ Facebook authentication is already active.'.data);
2014-04-13 01:37:55 +00:00
}
2014-04-13 02:54:30 +00:00
} else {
2014-04-13 04:48:24 +00:00
// config/passport.js
index = passportConfig.indexOf(facebookStrategyRequire);
passportConfig.splice(index, 1);
index = passportConfig.indexOf('// Sign in with Facebook.');
passportConfig.splice(index, 47);
2014-04-18 00:15:48 +00:00
fs.writeFileSync(passportConfigFile, passportConfig.join(os.EOL));
// views/account/login.jade
index = loginTemplate.indexOf(" a.btn.btn-block.btn-facebook.btn-social(href='/auth/facebook')");
2014-04-15 16:27:44 +00:00
loginTemplate.splice(index, 3);
2014-04-18 00:15:48 +00:00
fs.writeFileSync(loginTemplateFile, loginTemplate.join(os.EOL));
// views/account/profile.jade
index = profileTemplate.indexOf(" if user.facebook");
profileTemplate.splice(index - 1, 5);
2014-04-18 00:15:48 +00:00
fs.writeFileSync(profileTemplateFile, profileTemplate.join(os.EOL));
// models/User.js
index = userModel.indexOf(' facebook: String,');
userModel.splice(index, 1);
2014-04-18 00:15:48 +00:00
fs.writeFileSync(userModelFile, userModel.join(os.EOL));
// Remove Facebook from app.js
index = app.indexOf("app.get('/auth/facebook', passport.authenticate('facebook', { scope: ['email', 'user_location'] }));");
app.splice(index, 4);
2014-04-18 00:15:48 +00:00
fs.writeFileSync(appFile, app.join(os.EOL));
console.log('✗ Facebook authentication has been removed.'.error);
2014-04-13 01:37:55 +00:00
}
2014-04-13 19:23:10 +00:00
///////////////////////////
// GitHub Authentication //
///////////////////////////
2014-04-13 20:39:25 +00:00
var githubRoutes = M(function() {
/***
app.get('/auth/github', passport.authenticate('github'));
app.get('/auth/github/callback', passport.authenticate('github', { failureRedirect: '/login' }), function(req, res) {
res.redirect(req.session.returnTo || '/');
});
***/
});
var githubStrategyRequire = "var GitHubStrategy = require('passport-github').Strategy;";
var githubStrategy = M(function() {
/***
// Sign in with GitHub.
passport.use(new GitHubStrategy(secrets.github, function(req, accessToken, refreshToken, profile, done) {
if (req.user) {
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);
} 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.' });
2014-04-13 20:39:25 +00:00
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);
2014-04-13 20:39:25 +00:00
});
}
});
});
}
}));
***/
});
var githubButton = M(function() {
/***
a.btn.btn-block.btn-github.btn-social(href='/auth/github')
i.fa.fa-github
| Sign in with GitHub
***/
});
var githubLinkUnlink = M(function() {
/***
if user.github
p: a.text-danger(href='/account/unlink/github') Unlink your GitHub account
else
p: a(href='/auth/github') Link your GitHub account
***/
});
var githubModel = ' github: String,';
2014-04-13 20:39:25 +00:00
if (_.contains(answer.auth, 'GitHub')) {
2014-04-13 20:39:25 +00:00
if (passportConfig.indexOf(githubStrategyRequire) < 0) {
// config/passport.js
2014-04-13 20:39:25 +00:00
index = passportConfig.indexOf("var passport = require('passport');");
passportConfig.splice(index + 1, 0, githubStrategyRequire);
index = passportConfig.indexOf('passport.deserializeUser(function(id, done) {');
passportConfig.splice(index + 6, 0, githubStrategy);
2014-04-18 00:15:48 +00:00
fs.writeFileSync(passportConfigFile, passportConfig.join(os.EOL));
2014-04-13 20:39:25 +00:00
// views/account/login.jade
2014-04-13 20:39:25 +00:00
loginTemplate.push(githubButton);
2014-04-18 00:15:48 +00:00
fs.writeFileSync(loginTemplateFile, loginTemplate.join(os.EOL));
2014-04-13 20:39:25 +00:00
// views/account/profile.jade
2014-04-13 20:39:25 +00:00
index = profileTemplate.indexOf(' h3 Linked Accounts');
profileTemplate.splice(index + 1, 0, githubLinkUnlink);
2014-04-18 00:15:48 +00:00
fs.writeFileSync(profileTemplateFile, profileTemplate.join(os.EOL));
2014-04-13 20:39:25 +00:00
// models/User.js
2014-04-13 20:39:25 +00:00
index = userModel.indexOf(' tokens: Array,');
userModel.splice(index - 1, 0, githubModel);
2014-04-18 00:15:48 +00:00
fs.writeFileSync(userModelFile, userModel.join(os.EOL));
2014-04-13 20:39:25 +00:00
// Add GitHub to app.js
index = app.indexOf(' * OAuth routes for sign-in.');
app.splice(index + 3, 0, githubRoutes);
2014-04-18 00:15:48 +00:00
fs.writeFileSync(appFile, app.join(os.EOL));
2014-04-13 20:39:25 +00:00
console.log('✓ GitHub authentication has been added.'.info);
} else {
console.log('✓ GitHub authentication is already active.'.data);
2014-04-13 20:39:25 +00:00
}
} else {
// config/passport.js
2014-04-13 20:39:25 +00:00
index = passportConfig.indexOf(githubStrategyRequire);
passportConfig.splice(index, 1);
index = passportConfig.indexOf('// Sign in with GitHub.');
2014-04-13 20:47:56 +00:00
passportConfig.splice(index, 48);
2014-04-18 00:15:48 +00:00
fs.writeFileSync(passportConfigFile, passportConfig.join(os.EOL));
2014-04-13 20:39:25 +00:00
// views/account/login.jade
2014-04-13 20:39:25 +00:00
index = loginTemplate.indexOf(" a.btn.btn-block.btn-github.btn-social(href='/auth/github')");
2014-04-15 16:27:44 +00:00
loginTemplate.splice(index, 3);
2014-04-18 00:15:48 +00:00
fs.writeFileSync(loginTemplateFile, loginTemplate.join(os.EOL));
2014-04-13 20:39:25 +00:00
// views/account/profile.jade
2014-04-13 20:47:56 +00:00
index = profileTemplate.indexOf(' if user.github');
2014-04-13 20:39:25 +00:00
profileTemplate.splice(index - 1, 5);
2014-04-18 00:15:48 +00:00
fs.writeFileSync(profileTemplateFile, profileTemplate.join(os.EOL));
2014-04-13 20:39:25 +00:00
// models/User.js
2014-04-13 20:39:25 +00:00
index = userModel.indexOf(' github: String,');
userModel.splice(index, 1);
2014-04-18 00:15:48 +00:00
fs.writeFileSync(userModelFile, userModel.join(os.EOL));
2014-04-13 20:39:25 +00:00
// Remove GitHub from app.js
index = app.indexOf("app.get('/auth/github', passport.authenticate('github'));");
app.splice(index, 4);
2014-04-18 00:15:48 +00:00
fs.writeFileSync(appFile, app.join(os.EOL));
2014-04-13 20:39:25 +00:00
console.log('✗ GitHub authentication has been removed.'.error);
}
///////////////////////////
// Google Authentication //
///////////////////////////
2014-04-13 19:23:10 +00:00
var googleRoutes = M(function() {
/***
app.get('/auth/google', passport.authenticate('google', { scope: 'profile email' }));
app.get('/auth/google/callback', passport.authenticate('google', { failureRedirect: '/login' }), function(req, res) {
res.redirect(req.session.returnTo || '/');
});
***/
});
var googleStrategyRequire = "var GoogleStrategy = require('passport-google-oauth').OAuth2Strategy;";
var googleStrategy = M(function() {
/***
// Sign in with Google.
passport.use(new GoogleStrategy(secrets.google, function(req, accessToken, refreshToken, profile, done) {
if (req.user) {
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);
} 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.' });
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.' });
2014-04-13 19:23:10 +00:00
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);
2014-04-13 19:23:10 +00:00
});
}
});
});
}
}));
2014-04-13 19:23:10 +00:00
***/
});
2014-04-13 19:23:10 +00:00
var googleButton = M(function() {
/***
a.btn.btn-block.btn-google-plus.btn-social(href='/auth/google')
i.fa.fa-google-plus
| Sign in with Google
***/
});
var googleLinkUnlink = M(function() {
/***
2014-04-13 19:23:10 +00:00
if user.google
p: a.text-danger(href='/account/unlink/google') Unlink your Google account
else
p: a(href='/auth/google') Link your Google account
***/
});
var googleModel = ' google: String,';
2014-04-13 19:23:10 +00:00
if (_.contains(answer.auth, 'Google')) {
2014-04-13 19:23:10 +00:00
if (passportConfig.indexOf(googleStrategyRequire) < 0) {
// Add Google to config/passport.js
2014-04-13 19:23:10 +00:00
index = passportConfig.indexOf("var passport = require('passport');");
passportConfig.splice(index + 1, 0, googleStrategyRequire);
index = passportConfig.indexOf('passport.deserializeUser(function(id, done) {');
passportConfig.splice(index + 6, 0, googleStrategy);
2014-04-18 00:15:48 +00:00
fs.writeFileSync(passportConfigFile, passportConfig.join(os.EOL));
2014-04-13 19:23:10 +00:00
// Add Google to views/account/login.jade
2014-04-13 19:23:10 +00:00
loginTemplate.push(googleButton);
2014-04-18 00:15:48 +00:00
fs.writeFileSync(loginTemplateFile, loginTemplate.join(os.EOL));
2014-04-13 19:23:10 +00:00
// Add Google to views/account/profile.jade
2014-04-13 19:23:10 +00:00
index = profileTemplate.indexOf(' h3 Linked Accounts');
profileTemplate.splice(index + 1, 0, googleLinkUnlink);
2014-04-18 00:15:48 +00:00
fs.writeFileSync(profileTemplateFile, profileTemplate.join(os.EOL));
2014-04-13 19:23:10 +00:00
// Add Google to models/User.js
2014-04-13 19:23:10 +00:00
index = userModel.indexOf(' tokens: Array,');
userModel.splice(index - 1, 0, googleModel);
2014-04-18 00:15:48 +00:00
fs.writeFileSync(userModelFile, userModel.join(os.EOL));
2014-04-13 19:23:10 +00:00
// Add Google to app.js
index = app.indexOf(' * OAuth routes for sign-in.');
app.splice(index + 3, 0, googleRoutes);
2014-04-18 00:15:48 +00:00
fs.writeFileSync(appFile, app.join(os.EOL));
console.log('✓ Google authentication has been added.'.info);
2014-04-13 19:23:10 +00:00
} else {
console.log('✓ Google authentication is already active.'.data);
2014-04-13 19:23:10 +00:00
}
} else {
// Remove Google from config/passport.js
2014-04-13 19:23:10 +00:00
index = passportConfig.indexOf(googleStrategyRequire);
passportConfig.splice(index, 1);
index = passportConfig.indexOf('// Sign in with Google.');
passportConfig.splice(index, 46);
2014-04-18 00:15:48 +00:00
fs.writeFileSync(passportConfigFile, passportConfig.join(os.EOL));
2014-04-13 19:23:10 +00:00
// Remove Google from views/account/login.jade
2014-04-13 19:23:10 +00:00
index = loginTemplate.indexOf(" a.btn.btn-block.btn-google-plus.btn-social(href='/auth/google')");
2014-04-15 16:27:44 +00:00
loginTemplate.splice(index, 3);
2014-04-18 00:15:48 +00:00
fs.writeFileSync(loginTemplateFile, loginTemplate.join(os.EOL));
2014-04-13 19:23:10 +00:00
// Remove Google from views/account/profile.jade
2014-04-13 20:47:56 +00:00
index = profileTemplate.indexOf(' if user.google');
2014-04-13 19:23:10 +00:00
profileTemplate.splice(index - 1, 5);
2014-04-18 00:15:48 +00:00
fs.writeFileSync(profileTemplateFile, profileTemplate.join(os.EOL));
2014-04-13 19:23:10 +00:00
// Remove Google from models/User.js
2014-04-13 19:23:10 +00:00
index = userModel.indexOf(' google: String,');
userModel.splice(index, 1);
2014-04-18 00:15:48 +00:00
fs.writeFileSync(userModelFile, userModel.join(os.EOL));
2014-04-13 19:23:10 +00:00
// Remove Google from app.js
index = app.indexOf("app.get('/auth/google', passport.authenticate('google', { scope: 'profile email' }));");
app.splice(index, 4);
2014-04-18 00:15:48 +00:00
fs.writeFileSync(appFile, app.join(os.EOL));
console.log('✗ Google authentication has been removed.'.error);
2014-04-13 19:23:10 +00:00
}
2014-04-13 20:47:56 +00:00
////////////////////////////
// Twitter Authentication //
////////////////////////////
2014-04-13 20:47:56 +00:00
var twitterRoutes = M(function() {
/***
app.get('/auth/twitter', passport.authenticate('twitter'));
app.get('/auth/twitter/callback', passport.authenticate('twitter', { failureRedirect: '/login' }), function(req, res) {
res.redirect(req.session.returnTo || '/');
});
***/
});
var twitterStrategyRequire = "var TwitterStrategy = require('passport-twitter').Strategy;";
var twitterStrategy = M(function() {
/***
// 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) {
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);
} 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.' });
done(err, user);
2014-04-13 20:47:56 +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-04-13 20:47:56 +00:00
});
});
}
}));
***/
});
2014-04-13 20:47:56 +00:00
var twitterButton = M(function() {
/***
a.btn.btn-block.btn-twitter-plus.btn-social(href='/auth/twitter')
i.fa.fa-twitter
| Sign in with Twitter
***/
});
var twitterLinkUnlink = M(function() {
/***
2014-04-13 20:47:56 +00:00
if user.twitter
p: a.text-danger(href='/account/unlink/twitter') Unlink your Twitter account
else
p: a(href='/auth/twitter') Link your Twitter account
***/
});
var twitterModel = ' twitter: String,';
2014-04-13 20:47:56 +00:00
if (_.contains(answer.auth, 'Twitter')) {
2014-04-13 20:47:56 +00:00
if (passportConfig.indexOf(twitterStrategyRequire) < 0) {
// config/passport.js
2014-04-13 20:47:56 +00:00
index = passportConfig.indexOf("var passport = require('passport');");
passportConfig.splice(index + 1, 0, twitterStrategyRequire);
index = passportConfig.indexOf('passport.deserializeUser(function(id, done) {');
passportConfig.splice(index + 6, 0, twitterStrategy);
2014-04-18 00:15:48 +00:00
fs.writeFileSync(passportConfigFile, passportConfig.join(os.EOL));
2014-04-13 20:47:56 +00:00
// views/account/login.jade
2014-04-13 20:47:56 +00:00
loginTemplate.push(twitterButton);
2014-04-18 00:15:48 +00:00
fs.writeFileSync(loginTemplateFile, loginTemplate.join(os.EOL));
2014-04-13 20:47:56 +00:00
// views/account/profile.jade
2014-04-13 20:47:56 +00:00
index = profileTemplate.indexOf(' h3 Linked Accounts');
profileTemplate.splice(index + 1, 0, twitterLinkUnlink);
2014-04-18 00:15:48 +00:00
fs.writeFileSync(profileTemplateFile, profileTemplate.join(os.EOL));
2014-04-13 20:47:56 +00:00
// models/User.js
2014-04-13 20:47:56 +00:00
index = userModel.indexOf(' tokens: Array,');
userModel.splice(index - 1, 0, twitterModel);
2014-04-18 00:15:48 +00:00
fs.writeFileSync(userModelFile, userModel.join(os.EOL));
2014-04-13 20:47:56 +00:00
// Add Twitter to app.js
index = app.indexOf(' * OAuth routes for sign-in.');
app.splice(index + 3, 0, twitterRoutes);
2014-04-18 00:15:48 +00:00
fs.writeFileSync(appFile, app.join(os.EOL));
2014-04-13 20:47:56 +00:00
console.log('✓ Twitter authentication has been added.'.info);
} else {
console.log('✓ Twitter authentication is already active.'.data);
2014-04-13 20:47:56 +00:00
}
} else {
// Remove Twitter from config/passport.js
2014-04-13 20:47:56 +00:00
index = passportConfig.indexOf(twitterStrategyRequire);
passportConfig.splice(index, 1);
index = passportConfig.indexOf('// Sign in with Twitter.');
passportConfig.splice(index, 43);
2014-04-18 00:15:48 +00:00
fs.writeFileSync(passportConfigFile, passportConfig.join(os.EOL));
2014-04-13 20:47:56 +00:00
// Remove Twitter from views/account/login.jade
2014-04-13 20:47:56 +00:00
index = loginTemplate.indexOf(" a.btn.btn-block.btn-twitter.btn-social(href='/auth/twitter')");
2014-04-15 16:27:44 +00:00
loginTemplate.splice(index, 3);
2014-04-18 00:15:48 +00:00
fs.writeFileSync(loginTemplateFile, loginTemplate.join(os.EOL));
2014-04-13 20:47:56 +00:00
// Remove Twitter from views/account/profile.jade
2014-04-13 20:47:56 +00:00
index = profileTemplate.indexOf(' if user.twitter');
profileTemplate.splice(index - 1, 5);
2014-04-18 00:15:48 +00:00
fs.writeFileSync(profileTemplateFile, profileTemplate.join(os.EOL));
2014-04-13 20:47:56 +00:00
// Remove Twitter from models/User.js
2014-04-13 20:47:56 +00:00
index = userModel.indexOf(' twitter: String,');
userModel.splice(index, 1);
2014-04-18 00:15:48 +00:00
fs.writeFileSync(userModelFile, userModel.join(os.EOL));
2014-04-13 20:47:56 +00:00
// Remove Twitter from app.js
index = app.indexOf("app.get('/auth/twitter', passport.authenticate('twitter'));");
app.splice(index, 4);
2014-04-18 00:15:48 +00:00
fs.writeFileSync(appFile, app.join(os.EOL));
2014-04-13 20:47:56 +00:00
console.log('✗ Twitter authentication has been removed.'.error);
}
/////////////////////////////
// LinkedIn Authentication //
/////////////////////////////
var linkedinRoutes = M(function() {
/***
app.get('/auth/linkedin', passport.authenticate('linkedin', { state: 'SOME STATE' }));
app.get('/auth/linkedin/callback', passport.authenticate('linkedin', { failureRedirect: '/login' }), function(req, res) {
res.redirect(req.session.returnTo || '/');
});
***/
});
var linkedinStrategyRequire = "var LinkedInStrategy = require('passport-linkedin-oauth2').Strategy;";
var linkedinStrategy = M(function() {
/***
2014-04-15 15:27:50 +00:00
// Sign in with LinkedIn.
passport.use(new LinkedInStrategy(secrets.linkedin, function(req, accessToken, refreshToken, profile, done) {
if (req.user) {
User.findOne({ $or: [
{ linkedin: profile.id },
{ email: profile._json.emailAddress }
] }, function(err, existingUser) {
if (existingUser) {
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);
} else {
User.findById(req.user.id, function(err, user) {
user.linkedin = profile.id;
user.tokens.push({ kind: 'linkedin', accessToken: accessToken });
user.profile.name = user.profile.name || profile.displayName;
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 {
User.findOne({ linkedin: profile.id }, function(err, existingUser) {
if (existingUser) return done(null, existingUser);
User.findOne({ email: profile._json.emailAddress }, 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 LinkedIn manually from Account Settings.' });
2014-04-13 21:21:02 +00:00
done(err);
} else {
2014-04-15 15:27:50 +00:00
var user = new User();
user.linkedin = profile.id;
user.tokens.push({ kind: 'linkedin', accessToken: accessToken });
user.email = profile._json.emailAddress;
user.profile.name = profile.displayName;
user.profile.location = profile._json.location.name;
user.profile.picture = profile._json.pictureUrl;
user.profile.website = profile._json.publicProfileUrl;
user.save(function(err) {
done(err, user);
2014-04-13 21:21:02 +00:00
});
}
});
2014-04-15 15:27:50 +00:00
});
}
}));
***/
});
2014-04-13 21:21:02 +00:00
var linkedinButton = M(function() {
/***
2014-04-15 15:27:50 +00:00
a.btn.btn-block.btn-linkedin.btn-social(href='/auth/linkedin')
i.fa.fa-linkedin
| Sign in with LinkedIn
***/
});
var linkedinLinkUnlink = M(function() {
/***
2014-04-13 21:21:02 +00:00
2014-04-15 15:27:50 +00:00
if user.linkedin
p: a.text-danger(href='/account/unlink/linkedin') Unlink your LinkedIn account
else
p: a(href='/auth/linkedin') Link your LinkedIn account
***/
});
var linkedinModel = ' linkedin: String,';
2014-04-13 21:21:02 +00:00
if (_.contains(answer.auth, 'LinkedIn')) {
2014-04-13 21:21:02 +00:00
if (passportConfig.indexOf(linkedinStrategyRequire) < 0) {
2014-04-15 15:27:50 +00:00
// Add LinkedIn to passport.js
2014-04-13 21:21:02 +00:00
index = passportConfig.indexOf("var passport = require('passport');");
passportConfig.splice(index + 1, 0, linkedinStrategyRequire);
index = passportConfig.indexOf('passport.deserializeUser(function(id, done) {');
passportConfig.splice(index + 6, 0, linkedinStrategy);
2014-04-18 00:15:48 +00:00
fs.writeFileSync(passportConfigFile, passportConfig.join(os.EOL));
2014-04-13 21:21:02 +00:00
2014-04-15 15:27:50 +00:00
// Add LinkedIn to login.jade
2014-04-13 21:21:02 +00:00
loginTemplate.push(linkedinButton);
2014-04-18 00:15:48 +00:00
fs.writeFileSync(loginTemplateFile, loginTemplate.join(os.EOL));
2014-04-13 21:21:02 +00:00
2014-04-15 15:27:50 +00:00
// Add LinkedIn to profile.jade
2014-04-13 21:21:02 +00:00
index = profileTemplate.indexOf(' h3 Linked Accounts');
profileTemplate.splice(index + 1, 0, linkedinLinkUnlink);
2014-04-18 00:15:48 +00:00
fs.writeFileSync(profileTemplateFile, profileTemplate.join(os.EOL));
2014-04-13 21:21:02 +00:00
2014-04-15 15:27:50 +00:00
// Add LinkedIn to models/User.js
2014-04-13 21:21:02 +00:00
index = userModel.indexOf(' tokens: Array,');
userModel.splice(index - 1, 0, linkedinModel);
2014-04-18 00:15:48 +00:00
fs.writeFileSync(userModelFile, userModel.join(os.EOL));
2014-04-13 21:21:02 +00:00
// Add LinkedIn to app.js
index = app.indexOf(' * OAuth routes for sign-in.');
app.splice(index + 3, 0, linkedinRoutes);
2014-04-18 00:15:48 +00:00
fs.writeFileSync(appFile, app.join(os.EOL));
2014-04-13 21:21:02 +00:00
console.log('✓ LinkedIn authentication has been added.'.info);
} else {
console.log('✓ LinkedIn authentication is already active.'.data);
2014-04-13 21:21:02 +00:00
}
} else {
// Check if we have LinkedIn authentication to begin with.
if (passportConfig.indexOf(linkedinStrategyRequire) < 0) return;
2014-04-15 15:27:50 +00:00
// Removed LinkedIn from passport.js
2014-04-13 21:21:02 +00:00
index = passportConfig.indexOf(linkedinStrategyRequire);
passportConfig.splice(index, 1);
index = passportConfig.indexOf('// Sign in with LinkedIn.');
passportConfig.splice(index, 51);
2014-04-18 00:15:48 +00:00
fs.writeFileSync(passportConfigFile, passportConfig.join(os.EOL));
2014-04-13 21:21:02 +00:00
// Remove LinkedIn from login.jade
2014-04-13 21:21:02 +00:00
index = loginTemplate.indexOf(" a.btn.btn-block.btn-linkedin.btn-social(href='/auth/linkedin')");
2014-04-15 16:27:44 +00:00
loginTemplate.splice(index, 3);
2014-04-18 00:15:48 +00:00
fs.writeFileSync(loginTemplateFile, loginTemplate.join(os.EOL));
2014-04-13 21:21:02 +00:00
// Remove LinkedIn from profile.jade
2014-04-13 21:21:02 +00:00
index = profileTemplate.indexOf(' if user.linkedin');
profileTemplate.splice(index - 1, 5);
2014-04-18 00:15:48 +00:00
fs.writeFileSync(profileTemplateFile, profileTemplate.join(os.EOL));
2014-04-13 21:21:02 +00:00
// Remove LinkedIn from app.js
index = app.indexOf("app.get('/auth/linkedin', passport.authenticate('linkedin', { state: 'SOME STATE' }));");
app.splice(index, 4);
2014-04-18 00:15:48 +00:00
fs.writeFileSync(appFile, app.join(os.EOL));
// Remove LinkedIn from User.js
2014-04-13 21:21:02 +00:00
index = userModel.indexOf(' linkedin: String,');
userModel.splice(index, 1);
2014-04-18 00:15:48 +00:00
fs.writeFileSync(userModelFile, userModel.join(os.EOL));
2014-04-13 21:21:02 +00:00
console.log('✗ LinkedIn authentication has been removed.'.error);
}
//////////////////////////////
// Instagram Authentication //
//////////////////////////////
var instagramRoutes = M(function() {
/***
app.get('/auth/instagram', passport.authenticate('instagram'));
app.get('/auth/instagram/callback', passport.authenticate('instagram', { failureRedirect: '/login' }), function(req, res) {
res.redirect(req.session.returnTo || '/');
});
***/
});
var instagramSecrets = M(function() {
/***
instagram: {
clientID: process.env.INSTAGRAM_ID || 'Your Client ID',
clientSecret: process.env.INSTAGRAM_SECRET || 'Your Client Secret',
callbackURL: '/auth/instagram/callback',
passReqToCallback: true
},
2014-04-14 06:30:28 +00:00
***/
});
var instagramStrategyRequire = "var InstagramStrategy = require('passport-instagram').Strategy;";
var instagramStrategy = M(function() {
/***
// Sign in with Instagram.
2014-04-14 06:30:28 +00:00
passport.use(new InstagramStrategy(secrets.instagram,function(req, accessToken, refreshToken, profile, done) {
if (req.user) {
User.findOne({ $or: [{ instagram: profile.id }, { email: profile.email }] }, function(err, existingUser) {
if (existingUser) {
req.flash('errors', { msg: 'There is already an Instagram 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.instagram = profile.id;
user.tokens.push({ kind: 'instagram', accessToken: accessToken });
user.profile.name = user.profile.name || profile.displayName;
user.profile.picture = user.profile.picture || profile._json.data.profile_picture;
user.profile.website = user.profile.website || profile._json.data.website;
user.save(function(err) {
req.flash('info', { msg: 'Instagram account has been linked.' });
done(err, user);
2014-04-14 06:30:28 +00:00
});
});
}
});
} else {
User.findOne({ instagram: profile.id }, function(err, existingUser) {
if (existingUser) return done(null, existingUser);
var user = new User();
user.instagram = profile.id;
user.tokens.push({ kind: 'instagram', accessToken: accessToken });
user.profile.name = profile.displayName;
2014-04-22 19:00:58 +00:00
// Similar to Twitter API, assigns a temporary e-mail address
// to get on with the registration process. It can be changed later
// to a valid e-mail address in Profile Management.
profile.username + "@instagram.com";
user.profile.website = profile._json.data.website;
user.profile.picture = profile._json.data.profile_picture;
user.save(function(err) {
done(err, user);
2014-04-14 06:30:28 +00:00
});
});
}
}));
***/
});
var instagramButton = M(function() {
/***
a.btn.btn-block.btn-instagram.btn-social(href='/auth/instagram')
i.fa.fa-instagram
| Sign in with Instagram
***/
});
var instagramLinkUnlink = M(function() {
/***
if user.instagram
p: a.text-danger(href='/account/unlink/instagram') Unlink your Instagram account
else
p: a(href='/auth/instagram') Link your Instagram account
***/
});
var instagramModel = ' instagram: String,';
2014-04-14 06:30:28 +00:00
if (_.contains(answer.auth, 'Instagram')) {
2014-04-14 06:30:28 +00:00
if (passportConfig.indexOf(instagramStrategyRequire) < 0) {
// Add Instagram to passport.js
index = passportConfig.indexOf("var passport = require('passport');");
passportConfig.splice(index + 1, 0, instagramStrategyRequire);
index = passportConfig.indexOf('passport.deserializeUser(function(id, done) {');
passportConfig.splice(index + 6, 0, instagramStrategy);
2014-04-18 00:15:48 +00:00
fs.writeFileSync(passportConfigFile, passportConfig.join(os.EOL));
2014-04-14 06:30:28 +00:00
// Add Instagram to login.jade
loginTemplate.push(instagramButton);
2014-04-18 00:15:48 +00:00
fs.writeFileSync(loginTemplateFile, loginTemplate.join(os.EOL));
2014-04-14 06:30:28 +00:00
// Add Instagram to profile.jade
index = profileTemplate.indexOf(' h3 Linked Accounts');
profileTemplate.splice(index + 1, 0, instagramLinkUnlink);
2014-04-18 00:15:48 +00:00
fs.writeFileSync(profileTemplateFile, profileTemplate.join(os.EOL));
2014-04-14 06:30:28 +00:00
// Add Instagram to User.js
index = userModel.indexOf(' tokens: Array,');
userModel.splice(index - 1, 0, instagramModel);
2014-04-18 00:15:48 +00:00
fs.writeFileSync(userModelFile, userModel.join(os.EOL));
2014-04-14 06:30:28 +00:00
// Add Instagram to app.js
index = app.indexOf(' * OAuth routes for sign-in.');
app.splice(index + 3, 0, instagramRoutes);
2014-04-18 00:15:48 +00:00
fs.writeFileSync(appFile, app.join(os.EOL));
2014-04-14 06:30:28 +00:00
// Add Instagram to secrets.js
index = secrets.indexOf('module.exports = {');
secrets.splice(index + 1, 0, instagramSecrets);
2014-04-18 00:15:48 +00:00
fs.writeFileSync(secretsFile, secrets.join(os.EOL));
2014-04-14 06:30:28 +00:00
console.log('✓ Instagram authentication has been added.'.info);
} else {
console.log('✓ Instagram authentication is already active.'.data);
2014-04-14 06:30:28 +00:00
}
} else {
if (passportConfig.indexOf(instagramStrategyRequire) < 0) return;
2014-04-14 06:30:28 +00:00
// Remove Instagram from passport.js
index = passportConfig.indexOf(instagramStrategyRequire);
passportConfig.splice(index, 1);
index = passportConfig.indexOf('// Sign in with Instagram.');
passportConfig.splice(index, 40);
2014-04-18 00:15:48 +00:00
fs.writeFileSync(passportConfigFile, passportConfig.join(os.EOL));
2014-04-14 06:30:28 +00:00
// Remove Instagram from login.jade
index = loginTemplate.indexOf(" a.btn.btn-block.btn-instagram.btn-social(href='/auth/instagram')");
2014-04-15 16:27:44 +00:00
loginTemplate.splice(index, 3);
2014-04-18 00:15:48 +00:00
fs.writeFileSync(loginTemplateFile, loginTemplate.join(os.EOL));
2014-04-14 06:30:28 +00:00
// Remove Instagram from profile.jade
index = profileTemplate.indexOf(' if user.instagram');
profileTemplate.splice(index - 1, 5);
2014-04-18 00:15:48 +00:00
fs.writeFileSync(profileTemplateFile, profileTemplate.join(os.EOL));
2014-04-14 06:30:28 +00:00
// Remove Instagram from app.js
index = app.indexOf("app.get('/auth/instagram', passport.authenticate('instagram'));");
app.splice(index, 4);
2014-04-18 00:15:48 +00:00
fs.writeFileSync(appFile, app.join(os.EOL));
// Remove Instagram from secrets.js
index = secrets.indexOf(' instagram: {');
secrets.splice(index, 7);
2014-04-18 00:15:48 +00:00
fs.writeFileSync(secretsFile, secrets.join(os.EOL));
2014-04-14 06:30:28 +00:00
// Remove Instagram from User.js
index = userModel.indexOf(' instagram: String,');
userModel.splice(index, 1);
2014-04-18 00:15:48 +00:00
fs.writeFileSync(userModelFile, userModel.join(os.EOL));
2014-04-14 06:30:28 +00:00
console.log('✗ Instagram authentication has been removed.'.error);
}
2014-04-13 01:37:55 +00:00
});
}
});