From 7f8836daa470c3fb2a73f5e6ce83cac6a2caba9e Mon Sep 17 00:00:00 2001 From: psychobunny Date: Sat, 4 Jan 2014 17:00:52 -0500 Subject: [PATCH 1/3] simplified filter:server.create_routes - routes no longer require you to pass in req, res, name, route (the latter two was a mistake) tl;dr - just content is all that is required. see http://www.github.com/psychobunny/nodebb-plugin-static-page for more info --- src/webserver.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/webserver.js b/src/webserver.js index 035e8ae0b3..a2d6de53ef 100644 --- a/src/webserver.js +++ b/src/webserver.js @@ -839,8 +839,8 @@ var path = require('path'), app[routes[route].method || 'get'](routes[route].route, function(req, res) { routes[route].options(req, res, function(options) { app.build_header({ - req: options.req, - res: options.res + req: options.req || req, + res: options.res || res }, function (err, header) { res.send(header + options.content + templates.footer); }); From 751dc73ebee76df433a097da93709741652194df Mon Sep 17 00:00:00 2001 From: Baris Soner Usakli Date: Sat, 4 Jan 2014 17:10:56 -0500 Subject: [PATCH 2/3] fixes login/register and auth routes in relative path install --- public/src/app.js | 2 +- src/routes/admin.js | 2 +- src/routes/api.js | 1 + src/routes/authentication.js | 148 ++++++++++++++++++----------------- 4 files changed, 78 insertions(+), 75 deletions(-) diff --git a/public/src/app.js b/public/src/app.js index 437a017675..6d889381c2 100644 --- a/public/src/app.js +++ b/public/src/app.js @@ -23,7 +23,7 @@ var socket, } else { var max_reconnection_attemps = 5; var reconnection_delay = 200; - socket = io.connect(RELATIVE_PATH, { + socket = io.connect('', { 'max reconnection attempts': max_reconnection_attemps, 'reconnection delay': reconnection_delay }); diff --git a/src/routes/admin.js b/src/routes/admin.js index e2aab456d6..6a16b23710 100644 --- a/src/routes/admin.js +++ b/src/routes/admin.js @@ -20,7 +20,7 @@ var nconf = require('nconf'), Admin.isAdmin = function (req, res, next) { user.isAdministrator((req.user && req.user.uid) ? req.user.uid : 0, function (err, isAdmin) { if (!isAdmin) { - res.redirect('/403'); + res.redirect(403, '/403'); } else { next(); } diff --git a/src/routes/api.js b/src/routes/api.js index 3b464136a9..5ebe3dd9f1 100644 --- a/src/routes/api.js +++ b/src/routes/api.js @@ -18,6 +18,7 @@ var path = require('path'), (function (Api) { Api.createRoutes = function (app) { + app.namespace('/api', function () { app.get('/get_templates_listing', function (req, res) { utils.walk(path.join(__dirname, '../../', 'public/templates'), function (err, data) { diff --git a/src/routes/authentication.js b/src/routes/authentication.js index c3447fa47d..22039d0b1d 100644 --- a/src/routes/authentication.js +++ b/src/routes/authentication.js @@ -126,92 +126,94 @@ } Auth.createRoutes = function(app) { - app.post('/logout', function(req, res) { - if (req.user && req.user.uid > 0) { - winston.info('[Auth] Session ' + req.sessionID + ' logout (uid: ' + req.user.uid + ')'); + app.namespace(nconf.get('relative_path'), function () { + app.post('/logout', function(req, res) { + if (req.user && req.user.uid > 0) { + winston.info('[Auth] Session ' + req.sessionID + ' logout (uid: ' + req.user.uid + ')'); - var ws = require('./../websockets'); - ws.logoutUser(req.user.uid); + var ws = require('./../websockets'); + ws.logoutUser(req.user.uid); - req.logout(); - } - - res.send(200) - }); + req.logout(); + } - for (var i in login_strategies) { - var strategy = login_strategies[i]; - app.get(strategy.url, passport.authenticate(strategy.name, { - scope: strategy.scope - })); - - app.get(strategy.callbackURL, passport.authenticate(strategy.name, { - successRedirect: '/', - failureRedirect: '/login' - })); - } - - app.get('/reset/:code', function(req, res) { - app.build_header({ - req: req, - res: res - }, function(err, header) { - res.send(header + app.create_route('reset/' + req.params.code) + templates['footer']); + res.send(200) }); - }); - app.get('/reset', function(req, res) { - app.build_header({ - req: req, - res: res - }, function(err, header) { - res.send(header + app.create_route('reset') + templates['footer']); - }); - }); + for (var i in login_strategies) { + var strategy = login_strategies[i]; + app.get(strategy.url, passport.authenticate(strategy.name, { + scope: strategy.scope + })); - app.post('/login', function(req, res, next) { - passport.authenticate('local', function(err, user, info) { - if (err) { - return next(err); - } - if (!user) { - return res.send({ - success: false, - message: info.message - }); - } - req.login({ - uid: user.uid - }, function() { - res.send({ - success: true, - message: 'authentication succeeded' - }); + app.get(strategy.callbackURL, passport.authenticate(strategy.name, { + successRedirect: '/', + failureRedirect: '/login' + })); + } + + app.get('/reset/:code', function(req, res) { + app.build_header({ + req: req, + res: res + }, function(err, header) { + res.send(header + app.create_route('reset/' + req.params.code) + templates['footer']); }); - })(req, res, next); - }); + }); - app.post('/register', function(req, res) { - if(meta.config.allowRegistration !== undefined && parseInt(meta.config.allowRegistration, 10) === 0) { - return res.send(403); - } + app.get('/reset', function(req, res) { + app.build_header({ + req: req, + res: res + }, function(err, header) { + res.send(header + app.create_route('reset') + templates['footer']); + }); + }); - user.create(req.body.username, req.body.password, req.body.email, function(err, uid) { - if (err === null && uid) { + app.post('/login', function(req, res, next) { + passport.authenticate('local', function(err, user, info) { + if (err) { + return next(err); + } + if (!user) { + return res.send({ + success: false, + message: info.message + }); + } req.login({ - uid: uid + uid: user.uid }, function() { - - require('./../websockets').emitUserCount(); - - if(req.body.referrer) - res.redirect(req.body.referrer); - else - res.redirect(nconf.get('relative_path') + '/'); + res.send({ + success: true, + message: 'authentication succeeded' + }); }); - } else { - res.redirect(nconf.get('relative_path') + '/register'); + })(req, res, next); + }); + + app.post('/register', function(req, res) { + if(meta.config.allowRegistration !== undefined && parseInt(meta.config.allowRegistration, 10) === 0) { + return res.send(403); } + + user.create(req.body.username, req.body.password, req.body.email, function(err, uid) { + if (err === null && uid) { + req.login({ + uid: uid + }, function() { + + require('./../websockets').emitUserCount(); + + if(req.body.referrer) + res.redirect(req.body.referrer); + else + res.redirect(nconf.get('relative_path') + '/'); + }); + } else { + res.redirect(nconf.get('relative_path') + '/register'); + } + }); }); }); } From fda68bc5dd7f4ca79ca635e2a6c30966a8461664 Mon Sep 17 00:00:00 2001 From: psychobunny Date: Sat, 4 Jan 2014 17:44:08 -0500 Subject: [PATCH 3/3] added templates.setGlobal - allows you to add default template vars that are sent to all tpls upon parsing; added relative_path as global --- app.js | 2 ++ public/src/app.js | 2 ++ public/src/templates.js | 15 ++++++++++++++- 3 files changed, 18 insertions(+), 1 deletion(-) diff --git a/app.js b/app.js index 2c56822907..54b9b0c8d5 100644 --- a/app.js +++ b/app.js @@ -97,6 +97,8 @@ notifications = require('./src/notifications'), upgrade = require('./src/upgrade'); + templates.setGlobal('relative_path', nconf.get('relative_path')); + upgrade.check(function(schema_ok) { if (schema_ok || nconf.get('check-schema') === false) { websockets.init(SocketIO); diff --git a/public/src/app.js b/public/src/app.js index 6d889381c2..f379358e7a 100644 --- a/public/src/app.js +++ b/public/src/app.js @@ -468,6 +468,8 @@ var socket, app.alternatingTitle(''); }); + + templates.setGlobal('relative_path', RELATIVE_PATH); }); showWelcomeMessage = location.href.indexOf('loggedin') !== -1; diff --git a/public/src/templates.js b/public/src/templates.js index c451d982bf..23687bc515 100644 --- a/public/src/templates.js +++ b/public/src/templates.js @@ -6,7 +6,9 @@ available_templates = [], parsed_variables = {}; - module.exports = templates = {}; + module.exports = templates = { + "globals": {} + }; try { fs = require('fs'); @@ -242,6 +244,10 @@ parsed_variables[key] = value; } + templates.setGlobal = function(key, value) { + templates.globals[key] = value; + } + //modified from https://github.com/psychobunny/dcp.templates var parse = function (data) { var self = this; @@ -282,6 +288,13 @@ var template = this.html, regex, block; + // registering globals + for (var g in templates.globals) { + if (templates.globals.hasOwnProperty(g)) { + data[g] = data[g] || templates.globals[g]; + } + } + return (function parse(data, namespace, template, blockInfo) { if (!data || data.length == 0) { template = '';