From 89c5d01efa6b948b67bcb4cd19a608da1bdf11cc Mon Sep 17 00:00:00 2001 From: psychobunny Date: Wed, 27 Nov 2013 16:47:40 -0500 Subject: [PATCH] modularized SSO, paving the way for potential plugins. cleaned up associated templates/js --- public/src/forum/login.js | 19 +----------- public/src/forum/register.js | 5 ---- public/templates/login.tpl | 10 ++++--- public/templates/register.tpl | 11 ++++--- src/routes/api.js | 18 ++++++------ src/routes/authentication.js | 54 +++++++++++++++++------------------ 6 files changed, 48 insertions(+), 69 deletions(-) diff --git a/public/src/forum/login.js b/public/src/forum/login.js index a414f39e4e..7ab435b93f 100644 --- a/public/src/forum/login.js +++ b/public/src/forum/login.js @@ -1,24 +1,7 @@ define(function() { var Login = {}; - Login.init = function() { - // Alternate Logins - var altLoginEl = document.querySelector('.alt-logins'); - altLoginEl.addEventListener('click', function(e) { - var target; - switch (e.target.nodeName) { - case 'LI': - target = e.target; - break; - case 'I': - target = e.target.parentNode; - break; - } - if (target) { - document.location.href = target.getAttribute('data-url'); - } - }); - + Login.init = function() { $('#login').on('click', function() { var loginData = { 'username': $('#username').val(), diff --git a/public/src/forum/register.js b/public/src/forum/register.js index fb1959f5c5..d4b1781134 100644 --- a/public/src/forum/register.js +++ b/public/src/forum/register.js @@ -134,11 +134,6 @@ define(function() { } }); - // Alternate Logins - $('.alt-logins li').on('click', function(e) { - document.location.href = $(this).attr('data-url'); - }); - function validateForm() { validationError = false; diff --git a/public/templates/login.tpl b/public/templates/login.tpl index a3bcc5ef21..a37adcdfd6 100644 --- a/public/templates/login.tpl +++ b/public/templates/login.tpl @@ -49,14 +49,16 @@ -
+ +

[[login:alternative_logins]]

    - -
  • - + +
  • +
+
diff --git a/public/templates/register.tpl b/public/templates/register.tpl index 0d2dc3aaae..4dd4f6a56c 100644 --- a/public/templates/register.tpl +++ b/public/templates/register.tpl @@ -69,14 +69,17 @@ -
+ + +

[[register:alternative_registration]]

    - -
  • - + +
  • +
+
diff --git a/src/routes/api.js b/src/routes/api.js index 8c8cf5ecdd..b525dcc4be 100644 --- a/src/routes/api.js +++ b/src/routes/api.js @@ -72,18 +72,17 @@ var user = require('../user'), if (num_strategies == 0) { data = { 'login_window:spansize': 'col-md-12', - 'alternate_logins:display': 'none' + 'alternate_logins': false }; } else { data = { 'login_window:spansize': 'col-md-6', - 'alternate_logins:display': 'block' - } - for (var i = 0, ii = num_strategies; i < ii; i++) { - data[login_strategies[i] + ':display'] = 'active'; + 'alternate_logins': true } } + data.authentication = login_strategies; + data.token = res.locals.csrf_token; res.json(data); @@ -97,18 +96,17 @@ var user = require('../user'), if (num_strategies == 0) { data = { 'register_window:spansize': 'col-md-12', - 'alternate_logins:display': 'none' + 'alternate_logins': false }; } else { data = { 'register_window:spansize': 'col-md-6', - 'alternate_logins:display': 'block' - } - for (var i = 0, ii = num_strategies; i < ii; i++) { - data[login_strategies[i] + ':display'] = 'active'; + 'alternate_logins': true } } + data.authentication = login_strategies; + data.token = res.locals.csrf_token; data.minimumUsernameLength = meta.config['minimumUsernameLength']; data.maximumUsernameLength = meta.config['maximumUsernameLength']; diff --git a/src/routes/authentication.js b/src/routes/authentication.js index 9f50120461..7f21ee3cac 100644 --- a/src/routes/authentication.js +++ b/src/routes/authentication.js @@ -32,7 +32,13 @@ }); })); - login_strategies.push('twitter'); + login_strategies.push({ + name: 'twitter', + url: '/auth/twitter', + callbackURL: '/auth/twitter/callback', + icon: 'twitter', + scope: '' + }); } if (meta.config['social:google:id'] && meta.config['social:google:secret']) { @@ -49,7 +55,13 @@ }); })); - login_strategies.push('google'); + login_strategies.push({ + name: 'google', + url: '/auth/google', + callbackURL: '/auth/google/callback', + icon: 'google-plus', + scope: 'https://www.googleapis.com/auth/userinfo.profile https://www.googleapis.com/auth/userinfo.email' + }); } if (meta.config['social:facebook:app_id'] && meta.config['social:facebook:secret']) { @@ -66,7 +78,13 @@ }); })); - login_strategies.push('facebook'); + login_strategies.push({ + name: 'facebook', + url: '/auth/facebook', + callbackURL: '/auth/facebook/callback', + icon: 'facebook', + scope: 'email' + }); } passport.serializeUser(function(user, done) { @@ -103,38 +121,18 @@ res.send(200) }); - if (login_strategies.indexOf('twitter') !== -1) { - app.get('/auth/twitter', passport.authenticate('twitter')); - - app.get('/auth/twitter/callback', passport.authenticate('twitter', { - successRedirect: '/', - failureRedirect: '/login' - })); - } - - if (login_strategies.indexOf('google') !== -1) { - app.get('/auth/google', passport.authenticate('google', { - scope: 'https://www.googleapis.com/auth/userinfo.profile https://www.googleapis.com/auth/userinfo.email' + for (var i in login_strategies) { + var strategy = login_strategies[i]; + app.get(strategy.url, passport.authenticate(strategy.name, { + scope: strategy.scope })); - app.get('/auth/google/callback', passport.authenticate('google', { + app.get(strategy.callbackURL, passport.authenticate(strategy.name, { successRedirect: '/', failureRedirect: '/login' })); } - if (login_strategies.indexOf('facebook') !== -1) { - app.get('/auth/facebook', passport.authenticate('facebook', { - scope: 'email' - })); - - app.get('/auth/facebook/callback', passport.authenticate('facebook', { - successRedirect: '/', - failureRedirect: '/login' - })); - } - - app.get('/reset/:code', function(req, res) { app.build_header({