From a14263349bf50cd1b97cd254c5acb144a4d0c208 Mon Sep 17 00:00:00 2001 From: Julian Lam Date: Wed, 22 Jun 2016 16:47:24 -0400 Subject: [PATCH] updated termsOfUse to use registration interstitial instead of being integrated into register template --- public/language/en_GB/register.json | 5 ++++- src/controllers/authentication.js | 30 ++++++++++++++++++++++------- src/controllers/index.js | 2 ++ src/routes/authentication.js | 1 + src/routes/index.js | 4 +++- src/user.js | 28 +++++++++++++++++++++++++++ src/user/create.js | 2 -- 7 files changed, 61 insertions(+), 11 deletions(-) diff --git a/public/language/en_GB/register.json b/public/language/en_GB/register.json index f6ee52afd7..81b20421d4 100644 --- a/public/language/en_GB/register.json +++ b/public/language/en_GB/register.json @@ -1,5 +1,6 @@ { "register": "Register", + "cancel_registration": "Cancel Registration", "help.email": "By default, your email will be hidden from the public.", "help.username_restrictions": "A unique username between %1 and %2 characters. Others can mention you with @username.", "help.minimum_password_length": "Your password's length must be at least %1 characters.", @@ -15,6 +16,8 @@ "alternative_registration": "Alternative Registration", "terms_of_use": "Terms of Use", "agree_to_terms_of_use": "I agree to the Terms of Use", + "terms_of_use_error": "You must agree to the Terms of Use", "registration-added-to-queue": "Your registration has been added to the approval queue. You will receive an email when it is accepted by an administrator.", - "interstitial.intro": "We require some additional information before we can create your account." + "interstitial.intro": "We require some additional information before we can create your account.", + "interstitial.errors-found": "We could not complete your registration:" } \ No newline at end of file diff --git a/src/controllers/authentication.js b/src/controllers/authentication.js index 180c9e57c4..cea396c522 100644 --- a/src/controllers/authentication.js +++ b/src/controllers/authentication.js @@ -88,11 +88,11 @@ function registerAndLoginUser(req, res, userData, callback) { }, function(err, data) { // If interstitials are found, save registration attempt into session and abort var deferRegistration = data.interstitials.length; - deferRegistration = true; if (!deferRegistration) { return next(); } else { + userData.register = true; req.session.registration = userData; return res.json({ referrer: nconf.get('relative_path') + '/register/complete' }); } @@ -142,24 +142,40 @@ authenticationController.registerComplete = function(req, res, next) { return memo; }, []); + var done = function() { + delete req.session.registration; + + if (req.session.returnTo) { + res.redirect(req.session.returnTo); + } else { + res.redirect(nconf.get('relative_path') + '/'); + } + } + async.parallel(callbacks, function(err) { if (err) { req.flash('error', err.message); return res.redirect(nconf.get('relative_path') + '/register/complete'); } - // Clear registration data in session - delete req.session.registration; - - if (req.session.returnTo) { - res.redirect(req.session.returnTo); + if (req.session.registration.register === true) { + res.locals.processLogin = true; + registerAndLoginUser(req, res, req.session.registration, done); } else { - res.redirect(nconf.get('relative_path') + '/'); + // Clear registration data in session + done(); } }); }); }; +authenticationController.registerAbort = function(req, res, next) { + // End the session and redirect to home + req.session.destroy(function() { + res.redirect(nconf.get('relative_path') + '/'); + }); +}; + authenticationController.login = function(req, res, next) { // Handle returnTo data if (req.body.hasOwnProperty('returnTo') && !req.session.returnTo) { diff --git a/src/controllers/index.js b/src/controllers/index.js index 5906b36285..d55bc14a6c 100644 --- a/src/controllers/index.js +++ b/src/controllers/index.js @@ -201,9 +201,11 @@ Controllers.registerInterstitial = function(req, res, next) { var renders = data.interstitials.map(function(interstitial) { return async.apply(req.app.render.bind(req.app), interstitial.template, interstitial.data) }); + var errors = req.flash('error'); async.parallel(renders, function(err, sections) { res.render('registerComplete', { + errors: errors, sections: sections }); }); diff --git a/src/routes/authentication.js b/src/routes/authentication.js index ad6c0f0557..d8561f2528 100644 --- a/src/routes/authentication.js +++ b/src/routes/authentication.js @@ -65,6 +65,7 @@ router.post('/register', Auth.middleware.applyCSRF, Auth.middleware.applyBlacklist, controllers.authentication.register); router.post('/register/complete', Auth.middleware.applyCSRF, Auth.middleware.applyBlacklist, controllers.authentication.registerComplete); + router.get('/register/abort', controllers.authentication.registerAbort); router.post('/login', Auth.middleware.applyCSRF, Auth.middleware.applyBlacklist, controllers.authentication.login); router.post('/logout', Auth.middleware.applyCSRF, controllers.authentication.logout); diff --git a/src/routes/index.js b/src/routes/index.js index d250524e24..06c9706856 100644 --- a/src/routes/index.js +++ b/src/routes/index.js @@ -6,6 +6,7 @@ var async = require('async'); var winston = require('winston'); var controllers = require('../controllers'); var plugins = require('../plugins'); +var user = require('../user'); var express = require('express'); var validator = require('validator'); @@ -156,6 +157,7 @@ module.exports = function(app, middleware, hotswapIds) { // Add plugin routes async.series([ async.apply(plugins.reloadRoutes), - async.apply(authRoutes.reloadRoutes) + async.apply(authRoutes.reloadRoutes), + async.apply(user.addInterstitials) ]); }; diff --git a/src/user.js b/src/user.js index c7fb18e4fc..50df737bc7 100644 --- a/src/user.js +++ b/src/user.js @@ -6,6 +6,7 @@ var plugins = require('./plugins'); var db = require('./database'); var topics = require('./topics'); var privileges = require('./privileges'); +var meta = require('./meta'); var utils = require('../public/src/utils'); (function(User) { @@ -255,5 +256,32 @@ var utils = require('../public/src/utils'); }); }; + User.addInterstitials = function(callback) { + plugins.registerHook('core', { + hook: 'filter:register.interstitial', + method: function(data, callback) { + if (meta.config.termsOfUse && !data.userData.acceptTos) { + data.interstitials.push({ + template: 'partials/acceptTos', + data: { + termsOfUse: meta.config.termsOfUse + }, + callback: function(userData, formData, next) { + if (formData['agree-terms'] === 'on') { + userData.acceptTos = true; + } + + next(userData.acceptTos ? null : new Error('[[register:terms_of_use_error]]')); + } + }); + } + + callback(null, data); + } + }); + + callback(); + }; + }(exports)); diff --git a/src/user/create.js b/src/user/create.js index ae69f1ad5d..bd79270a93 100644 --- a/src/user/create.js +++ b/src/user/create.js @@ -8,11 +8,9 @@ var plugins = require('../plugins'); var groups = require('../groups'); var meta = require('../meta'); - module.exports = function(User) { User.create = function(data, callback) { - data.username = data.username.trim(); data.userslug = utils.slugify(data.username); if (data.email !== undefined) {