diff --git a/public/src/app.js b/public/src/app.js index edf22a0c50..9ae50d4d87 100644 --- a/public/src/app.js +++ b/public/src/app.js @@ -24,7 +24,7 @@ var socket, reconnecting = false; // Rejoin room that was left when we disconnected - var url_parts = document.location.pathname.slice(RELATIVE_PATH.length).split('/').slice(1); + var url_parts = window.location.pathname.slice(RELATIVE_PATH.length).split('/').slice(1); var room; switch(url_parts[0]) { @@ -512,7 +512,10 @@ var socket, app.load = function() { $('document').ready(function () { var url = window.location.pathname.slice(1), - tpl_url = ajaxify.getTemplateMapping(url); + search = window.location.search, + hash = window.location.hash, + tpl_url = ajaxify.getTemplateMapping(url), + $window = $(window); url = url.replace(/\/$/, ""); @@ -520,7 +523,7 @@ var socket, url = url.slice(RELATIVE_PATH.length); } - $(window).trigger('action:ajaxify.start', { + $window.trigger('action:ajaxify.start', { url: url }); @@ -532,11 +535,11 @@ var socket, $('#logout-link').on('click', app.logout); - $(window).blur(function(){ + $window.blur(function(){ app.isFocused = false; }); - $(window).focus(function(){ + $window.focus(function(){ app.isFocused = true; app.alternatingTitle(''); }); @@ -549,21 +552,20 @@ var socket, ajaxify.widgets.render(tpl_url, url); if (window.history && window.history.replaceState) { - var hash = window.location.hash ? window.location.hash : ''; window.history.replaceState({ - url: url + hash - }, url, RELATIVE_PATH + '/' + url + hash); + url: url + search + hash + }, url, RELATIVE_PATH + '/' + url + search + hash); } ajaxify.loadScript(tpl_url, function() { - $(window).trigger('action:ajaxify.end', { + $window.trigger('action:ajaxify.end', { url: url }); }); }); }; - showWelcomeMessage = location.href.indexOf('loggedin') !== -1; + showWelcomeMessage = window.location.href.indexOf('loggedin') !== -1; app.loadConfig(); app.alternatingTitle(''); diff --git a/public/src/utils.js b/public/src/utils.js index 534ef59273..944929e486 100644 --- a/public/src/utils.js +++ b/public/src/utils.js @@ -272,6 +272,87 @@ return env; } } + }, + + // get all the url params in a single key/value hash + params: function(options) { + var a, hash = {}, params; + + options = options || {}; + options.skipToType = options.skipToType || {}; + + if (options.url) { + a = utils.urlToLocation(options.url); + } + params = (a ? a.search : window.location.search).substring(1).split("&"); + + params.forEach(function(param) { + var val = param.split('='), + key = decodeURI(val[0]), + value = options.skipToType[key] ? decodeURI(val[1]) : utils.toType(decodeURI(val[1])); + + if (key) + hash[key] = value; + }); + return hash; + }, + + param: function(key) { + return this.params()[key]; + }, + + urlToLocation: function(url) { + var a = document.createElement('a'); + a.href = url; + return a; + }, + + // return boolean if string 'true' or string 'false', or if a parsable string which is a number + // also supports JSON object and/or arrays parsing + toType: function(str) { + var type = typeof str; + if (type !== 'string') { + return str; + } else { + var nb = parseFloat(str); + if (!isNaN(nb) && isFinite(str)) + return nb; + if (str === 'false') + return false; + if (str === 'true') + return true; + + try { + str = JSON.parse(str); + } catch (e) {} + + return str; + } + }, + + // Safely get/set chained properties on an object + // set example: utils.props(A, 'a.b.c.d', 10) // sets A to {a: {b: {c: {d: 10}}}}, and returns 10 + // get example: utils.props(A, 'a.b.c') // returns {d: 10} + // get example: utils.props(A, 'a.b.c.foo.bar') // returns undefined without throwing a TypeError + // credits to github.com/gkindel + props: function(obj, props, value) { + if(obj === undefined) + obj = window; + if(props == null) + return undefined; + var i = props.indexOf('.'); + if( i == -1 ) { + if(value !== undefined) + obj[props] = value; + return obj[props]; + } + var prop = props.slice(0, i), + newProps = props.slice(i + 1); + + if(props !== undefined && !(obj[prop] instanceof Object) ) + obj[prop] = {}; + + return util.props(obj[prop], newProps, value); } }; diff --git a/src/controllers/index.js b/src/controllers/index.js index 47ea0dcd88..528472afb7 100644 --- a/src/controllers/index.js +++ b/src/controllers/index.js @@ -207,7 +207,12 @@ Controllers.register = function(req, res, next) { data.minimumPasswordLength = meta.config.minimumPasswordLength; data.termsOfUse = meta.config.termsOfUse; - res.render('register', data); + plugins.fireHook('filter:register.build', req, res, data, function(err, data) { + if (err && process.env === 'development') { + winston.warn(JSON.stringify(err)); + } + res.render('register', data); + }); }; diff --git a/src/plugins.js b/src/plugins.js index c72420def7..a1c715711b 100644 --- a/src/plugins.js +++ b/src/plugins.js @@ -295,14 +295,14 @@ var fs = require('fs'), }; Plugins.fireHook = function(hook) { - var callback = typeof arguments[arguments.length-1] === "function" ? arguments[arguments.length-1] : null, + var callback = typeof arguments[arguments.length-1] === 'function' ? arguments[arguments.length-1] : null, args = arguments.length ? Array.prototype.slice.call(arguments, 1) : []; if (callback) { args.pop(); } - hookList = Plugins.loadedHooks[hook]; + var hookList = Plugins.loadedHooks[hook]; if (hookList && Array.isArray(hookList)) { // if (global.env === 'development') winston.info('[plugins] Firing hook: \'' + hook + '\''); @@ -334,7 +334,7 @@ var fs = require('fs'), }, function(err, values) { if (err) { if (global.env === 'development') { - winston.info('[plugins] Problem executing hook: ' + hook); + winston.info('[plugins] Problem executing hook: ' + hook + ' err: ' + JSON.stringify(err)); } } diff --git a/src/routes/authentication.js b/src/routes/authentication.js index b203952b99..c008dd2b80 100644 --- a/src/routes/authentication.js +++ b/src/routes/authentication.js @@ -73,13 +73,12 @@ var userData = { username: req.body.username, password: req.body.password, - email: req.body.email, - ip: req.ip + email: req.body.email }; - plugins.fireHook('filter:register.check', userData, function(err, userData) { + plugins.fireHook('filter:register.check', req, res, userData, function(err, userData) { if (err) { - return res.redirect(nconf.get('relative_path') + '/register'); + return res.redirect(nconf.get('relative_path') + '/register' + (err.message ? '?error=' + err.message : '')); } user.create(userData, function(err, uid) {