diff --git a/config/passport.js b/config/passport.js index 8ec41ce4e6..f4e510e877 100644 --- a/config/passport.js +++ b/config/passport.js @@ -127,72 +127,6 @@ passport.use(new LinkedInStrategy(secrets.linkedin, function(req, accessToken, r } })); - -// Login Required middleware. - -module.exports = { - isAuthenticated: isAuthenticated, - isAuthorized: isAuthorized -}; - -function isAuthenticated(req, res, next) { - if (req.isAuthenticated()) return next(); - res.redirect('/login'); -} - -// Authorization Required middleware. -function isAuthorized(req, res, next) { - var provider = req.path.split('/').slice(-1)[0]; - - if (_.find(req.user.tokens, { kind: provider })) { - next(); - } else { - res.redirect('/auth/' + provider); - } -} - -/* -passport.use(new InstagramStrategy(secrets.instagram,function(req, accessToken, refreshToken, profile, done) { - if (req.user) { - User.findOne({ instagram: profile.id }, 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); - }); - }); - } - }); - } 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; - // 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. - user.email = 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); - }); - }); - } -})); - // Sign in using Email and Password. passport.use(new LocalStrategy({ usernameField: 'email' }, function(email, password, done) { User.findOne({ email: email }, function(err, user) { @@ -351,6 +285,72 @@ passport.use(new GoogleStrategy(secrets.google, function(req, accessToken, refre })); +// Login Required middleware. + +module.exports = { + isAuthenticated: isAuthenticated, + isAuthorized: isAuthorized +}; + +function isAuthenticated(req, res, next) { + if (req.isAuthenticated()) return next(); + res.redirect('/login'); +} + +// Authorization Required middleware. +function isAuthorized(req, res, next) { + var provider = req.path.split('/').slice(-1)[0]; + + if (_.find(req.user.tokens, { kind: provider })) { + next(); + } else { + res.redirect('/auth/' + provider); + } +} + +/* +passport.use(new InstagramStrategy(secrets.instagram,function(req, accessToken, refreshToken, profile, done) { + if (req.user) { + User.findOne({ instagram: profile.id }, 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); + }); + }); + } + }); + } 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; + // 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. + user.email = 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); + }); + }); + } +})); + + // Tumblr API setup. passport.use('tumblr', new OAuthStrategy({