diff --git a/src/meta.js b/src/meta.js index 09e9a6d134..be696ca98f 100644 --- a/src/meta.js +++ b/src/meta.js @@ -5,7 +5,8 @@ var async = require('async'), user = require('./user'), groups = require('./groups'), plugins = require('./plugins'), - emitter = require('./emitter'); + emitter = require('./emitter'), + auth = require('./routes/authentication'); (function (Meta) { Meta.restartRequired = false; @@ -36,7 +37,8 @@ var async = require('async'), async.parallel([ async.apply(Meta.js.minify, false), async.apply(Meta.css.minify), - async.apply(Meta.templates.compile) + async.apply(Meta.templates.compile), + async.apply(auth.reloadRoutes) ], next); } ], function(err) { diff --git a/src/routes/authentication.js b/src/routes/authentication.js index aa59419a6a..4f3e497748 100644 --- a/src/routes/authentication.js +++ b/src/routes/authentication.js @@ -7,14 +7,17 @@ Password = require('../password'), winston = require('winston'), async = require('async'), + express = require('express'), meta = require('../meta'), user = require('../user'), plugins = require('../plugins'), db = require('../database'), + hotswap = require('../hotswap'), utils = require('../../public/src/utils'), - login_strategies = []; + login_strategies = [], + controllers = require('../controllers'); function logout(req, res) { if (req.user && parseInt(req.user.uid, 10) > 0) { @@ -140,17 +143,21 @@ app.use(passport.session()); }; - Auth.get_login_strategies = function() { return login_strategies; }; - Auth.registerApp = function(app) { + Auth.registerApp = function(app, middleware) { Auth.app = app; + Auth.middleware = middleware; }; - Auth.createRoutes = function(app, middleware, controllers) { + Auth.reloadRoutes = function(callback) { + var router = express.Router(); + router.hotswapId = 'auth'; + plugins.ready(function() { + console.log('reloading auth routes!'); plugins.fireHook('filter:auth.init', login_strategies, function(err) { if (err) { winston.error('filter:auth.init - plugin failure'); @@ -173,12 +180,12 @@ /* End backwards compatibility block */ if (strategy.url) { - app.get(strategy.url, passport.authenticate(strategy.name, { + router.get(strategy.url, passport.authenticate(strategy.name, { scope: strategy.scope })); } - app.get(strategy.callbackURL, passport.authenticate(strategy.name, { + router.get(strategy.callbackURL, passport.authenticate(strategy.name, { successReturnToOrRedirect: nconf.get('relative_path') + '/', failureRedirect: nconf.get('relative_path') + '/login' })); @@ -198,9 +205,15 @@ } /* End backwards compatibility block */ - app.post('/logout', logout); - app.post('/register', middleware.applyCSRF, register); - app.post('/login', middleware.applyCSRF, login); + router.post('/logout', logout); + router.post('/register', Auth.middleware.applyCSRF, register); + router.post('/login', Auth.middleware.applyCSRF, login); + + hotswap.replace('auth', router); + console.log('now I\m here,', typeof callback); + if (typeof callback === 'function') { + callback(); + } }); }); }; diff --git a/src/routes/index.js b/src/routes/index.js index 85090aaf2e..0cd9a65d9e 100644 --- a/src/routes/index.js +++ b/src/routes/index.js @@ -154,12 +154,16 @@ module.exports = function(app, middleware) { var router = express.Router(), pageRouter = express.Router(), pluginRouter = express.Router(), + authRouter = express.Router(), relativePath = nconf.get('relative_path'); pluginRouter.render = function() { app.render.apply(app, arguments); }; + + // Set-up for hotswapping (when NodeBB reloads) pluginRouter.hotswapId = 'plugins'; + authRouter.hotswapId = 'auth'; app.all(relativePath + '/api/?*', middleware.prepareAPI); app.all(relativePath + '/api/admin/*', middleware.admin.isAdmin, middleware.prepareAPI); @@ -170,7 +174,6 @@ module.exports = function(app, middleware) { apiRoutes(router, middleware, controllers); feedRoutes(router, middleware, controllers); pluginRoutes(router, middleware, controllers); - authRoutes.createRoutes(router, middleware, controllers); app.use(relativePath, express.static(path.join(__dirname, '../../', 'public'), { maxAge: app.enabled('cache') ? 5184000000 : 0 @@ -195,6 +198,7 @@ module.exports = function(app, middleware) { app.use(relativePath, router); app.use(relativePath, pluginRouter); app.use(relativePath, pageRouter); + app.use(relativePath, authRouter); if (process.env.NODE_ENV === 'development') { require('./debug')(app, middleware, controllers); @@ -204,6 +208,7 @@ module.exports = function(app, middleware) { // Add plugin routes plugins.reloadRoutes(); + authRoutes.reloadRoutes(); }; function handleErrors(err, req, res, next) { diff --git a/src/webserver.js b/src/webserver.js index fa4b1e7fdc..e51ffe1da9 100644 --- a/src/webserver.js +++ b/src/webserver.js @@ -37,7 +37,6 @@ if(nconf.get('ssl')) { var port = nconf.get('PORT') || nconf.get('port'); logger.init(app); - auth.registerApp(app); emailer.registerApp(app); if (cluster.isWorker && process.env.handle_jobs === 'true') { @@ -64,6 +63,7 @@ if(nconf.get('ssl')) { middleware = middleware(app, data); routes(app, middleware); plugins.prepareApp(app, middleware); + auth.registerApp(app, middleware); if (err) { winston.error('Errors were encountered while attempting to initialise NodeBB.');