From 60c1e937a1368adaba0147baad7797a51b061a34 Mon Sep 17 00:00:00 2001 From: Julian Lam Date: Thu, 2 May 2013 12:13:06 -0400 Subject: [PATCH 1/2] tweaking twitter login so that if the config is blank, trying to log in via twitter won't crash the server --- public/templates/footer.tpl | 1 - public/templates/login.tpl | 3 +-- src/webserver.js | 39 ++++++++++++++++++++++--------------- 3 files changed, 24 insertions(+), 19 deletions(-) diff --git a/public/templates/footer.tpl b/public/templates/footer.tpl index 08fb9d2fb7..dd3de05b00 100644 --- a/public/templates/footer.tpl +++ b/public/templates/footer.tpl @@ -45,7 +45,6 @@ }); socket.emit('api:user.get', { fields: ['username', 'picture'] }); socket.on('api:user.get', function(data) { - console.log(data); if (data.uid > 0) { var gravatar = document.createElement('img'), name = document.createElement('span') diff --git a/public/templates/login.tpl b/public/templates/login.tpl index 8cf6aca923..d2f06bef2b 100644 --- a/public/templates/login.tpl +++ b/public/templates/login.tpl @@ -9,9 +9,8 @@


-   +   Forgot Password?
- Forgot Password?

Alternative Logins

diff --git a/src/webserver.js b/src/webserver.js index 586c462a9c..95d55d9d29 100644 --- a/src/webserver.js +++ b/src/webserver.js @@ -8,7 +8,8 @@ var express = require('express'), redisServer = redis.createClient(config.redis.port, config.redis.host, config.redis.options), passport = require('passport'), passportLocal = require('passport-local').Strategy, - passportTwitter = require('passport-twitter').Strategy; + passportTwitter = require('passport-twitter').Strategy, + login_strategies = []; passport.use(new passportLocal(function(user, password, next) { global.modules.user.loginViaLocal(user, password, function(login) { @@ -17,16 +18,20 @@ passport.use(new passportLocal(function(user, password, next) { }); })); -passport.use(new passportTwitter({ - consumerKey: config.twitter.key, - consumerSecret: config.twitter.secret, - callbackURL: config.url + "auth/twitter/callback" -}, function(token, tokenSecret, profile, done) { - global.modules.user.loginViaTwitter(profile.id, profile.username, function(err, user) { - if (err) { return done(err); } - done(null, user); - }); -})); +if (config.twitter.key.length > 0 && config.twitter.secret.length > 0) { + passport.use(new passportTwitter({ + consumerKey: config.twitter.key, + consumerSecret: config.twitter.secret, + callbackURL: config.url + "auth/twitter/callback" + }, function(token, tokenSecret, profile, done) { + global.modules.user.loginViaTwitter(profile.id, profile.username, function(err, user) { + if (err) { return done(err); } + done(null, user); + }); + })); + + login_strategies.push('twitter'); +} passport.serializeUser(function(user, done) { done(null, user.uid); @@ -147,12 +152,14 @@ passport.deserializeUser(function(uid, done) { }); }); - app.get('/auth/twitter', passport.authenticate('twitter')); + if (login_strategies.indexOf('twitter') !== -1) { + app.get('/auth/twitter', passport.authenticate('twitter')); - app.get('/auth/twitter/callback', passport.authenticate('twitter', { - successRedirect: '/', - failureRedirect: '/login' - })); + app.get('/auth/twitter/callback', passport.authenticate('twitter', { + successRedirect: '/', + failureRedirect: '/login' + })); + } app.get('/reset/:code', function(req, res) { res.send(templates['header'] + templates['reset_code'].parse({ reset_code: req.params.code }) + templates['footer']); From c73feeea2a9577e9e24a6498c21c6396b11041fa Mon Sep 17 00:00:00 2001 From: psychobunny Date: Thu, 2 May 2013 16:36:26 +0000 Subject: [PATCH 2/2] hide alternate login strategies if config is not set --- app.js | 7 ++----- public/css/style.less | 7 +++++++ public/templates/login.tpl | 6 +++--- src/templates.js | 4 ++-- src/webserver.js | 24 +++++++++++++++++++++++- 5 files changed, 37 insertions(+), 11 deletions(-) diff --git a/app.js b/app.js index b9759de5bc..96b367830c 100644 --- a/app.js +++ b/app.js @@ -4,7 +4,8 @@ var modules = { posts: require('./src/posts.js'), templates: require('./src/templates.js'), webserver: require('./src/webserver.js'), - websockets: require('./src/websockets.js') + websockets: require('./src/websockets.js'), + fs: require('fs') } DEVELOPMENT = true; @@ -13,9 +14,6 @@ var modules = { global.configuration = {}; global.modules = modules; -// change this to = null when auth module is complete -// global.uid = 1; - process.on('uncaughtException', function(err) { // handle the error safely @@ -28,7 +26,6 @@ process.on('uncaughtException', function(err) { config['ROOT_DIRECTORY'] = __dirname; modules.templates.init(); - // modules.webserver.init(); modules.websockets.init(); diff --git a/public/css/style.less b/public/css/style.less index 06d0cda1e8..19fc47425e 100644 --- a/public/css/style.less +++ b/public/css/style.less @@ -168,4 +168,11 @@ footer.footer { .inline-block; .pointer; } +} + +.none { + display: none !important; +} +.block { + display: block !important; } \ No newline at end of file diff --git a/public/templates/login.tpl b/public/templates/login.tpl index d2f06bef2b..304b8bd180 100644 --- a/public/templates/login.tpl +++ b/public/templates/login.tpl @@ -1,6 +1,6 @@

Login

-
+

Login via Username & Password

-
+

Alternative Logins

    -
  • +
\ No newline at end of file diff --git a/src/templates.js b/src/templates.js index 98e0ed6f99..e56f09f39d 100644 --- a/src/templates.js +++ b/src/templates.js @@ -1,4 +1,4 @@ -var fs = require('fs'); + (function(Templates) { @@ -7,7 +7,7 @@ var fs = require('fs'); function loadTemplates(templatesToLoad) { for (var t in templatesToLoad) { (function(file) { - fs.readFile(global.configuration.ROOT_DIRECTORY + '/public/templates/' + file + '.tpl', function(err, html) { + modules.fs.readFile(global.configuration.ROOT_DIRECTORY + '/public/templates/' + file + '.tpl', function(err, html) { var template = function() { this.toString = function() { return this.html; diff --git a/src/webserver.js b/src/webserver.js index 95d55d9d29..789810caf6 100644 --- a/src/webserver.js +++ b/src/webserver.js @@ -18,7 +18,7 @@ passport.use(new passportLocal(function(user, password, next) { }); })); -if (config.twitter.key.length > 0 && config.twitter.secret.length > 0) { +if (config.twitter && config.twitter.key && config.twitter.key.length > 0 && config.twitter.secret.length > 0) { passport.use(new passportTwitter({ consumerKey: config.twitter.key, consumerSecret: config.twitter.secret, @@ -125,6 +125,28 @@ passport.deserializeUser(function(uid, done) { res.send(JSON.stringify(data)); }); break; + case 'login' : + var data = {}, + num_strategies = login_strategies.length; + + if (num_strategies == 0) { + data = { + 'login_window:spansize': 'span12', + 'alternate_logins:display': 'none' + }; + } else { + data = { + 'login_window:spansize': 'span6', + 'alternate_logins:display': 'block' + } + for (var i=0, ii=num_strategies; i