re: #2108, auth re-routing

v1.18.x
Julian Lam 10 years ago
parent ac301eaabe
commit dab3667773

@ -5,7 +5,8 @@ var async = require('async'),
user = require('./user'), user = require('./user'),
groups = require('./groups'), groups = require('./groups'),
plugins = require('./plugins'), plugins = require('./plugins'),
emitter = require('./emitter'); emitter = require('./emitter'),
auth = require('./routes/authentication');
(function (Meta) { (function (Meta) {
Meta.restartRequired = false; Meta.restartRequired = false;
@ -36,7 +37,8 @@ var async = require('async'),
async.parallel([ async.parallel([
async.apply(Meta.js.minify, false), async.apply(Meta.js.minify, false),
async.apply(Meta.css.minify), async.apply(Meta.css.minify),
async.apply(Meta.templates.compile) async.apply(Meta.templates.compile),
async.apply(auth.reloadRoutes)
], next); ], next);
} }
], function(err) { ], function(err) {

@ -7,14 +7,17 @@
Password = require('../password'), Password = require('../password'),
winston = require('winston'), winston = require('winston'),
async = require('async'), async = require('async'),
express = require('express'),
meta = require('../meta'), meta = require('../meta'),
user = require('../user'), user = require('../user'),
plugins = require('../plugins'), plugins = require('../plugins'),
db = require('../database'), db = require('../database'),
hotswap = require('../hotswap'),
utils = require('../../public/src/utils'), utils = require('../../public/src/utils'),
login_strategies = []; login_strategies = [],
controllers = require('../controllers');
function logout(req, res) { function logout(req, res) {
if (req.user && parseInt(req.user.uid, 10) > 0) { if (req.user && parseInt(req.user.uid, 10) > 0) {
@ -140,17 +143,21 @@
app.use(passport.session()); app.use(passport.session());
}; };
Auth.get_login_strategies = function() { Auth.get_login_strategies = function() {
return login_strategies; return login_strategies;
}; };
Auth.registerApp = function(app) { Auth.registerApp = function(app, middleware) {
Auth.app = app; 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() { plugins.ready(function() {
console.log('reloading auth routes!');
plugins.fireHook('filter:auth.init', login_strategies, function(err) { plugins.fireHook('filter:auth.init', login_strategies, function(err) {
if (err) { if (err) {
winston.error('filter:auth.init - plugin failure'); winston.error('filter:auth.init - plugin failure');
@ -173,12 +180,12 @@
/* End backwards compatibility block */ /* End backwards compatibility block */
if (strategy.url) { if (strategy.url) {
app.get(strategy.url, passport.authenticate(strategy.name, { router.get(strategy.url, passport.authenticate(strategy.name, {
scope: strategy.scope scope: strategy.scope
})); }));
} }
app.get(strategy.callbackURL, passport.authenticate(strategy.name, { router.get(strategy.callbackURL, passport.authenticate(strategy.name, {
successReturnToOrRedirect: nconf.get('relative_path') + '/', successReturnToOrRedirect: nconf.get('relative_path') + '/',
failureRedirect: nconf.get('relative_path') + '/login' failureRedirect: nconf.get('relative_path') + '/login'
})); }));
@ -198,9 +205,15 @@
} }
/* End backwards compatibility block */ /* End backwards compatibility block */
app.post('/logout', logout); router.post('/logout', logout);
app.post('/register', middleware.applyCSRF, register); router.post('/register', Auth.middleware.applyCSRF, register);
app.post('/login', middleware.applyCSRF, login); router.post('/login', Auth.middleware.applyCSRF, login);
hotswap.replace('auth', router);
console.log('now I\m here,', typeof callback);
if (typeof callback === 'function') {
callback();
}
}); });
}); });
}; };

@ -154,12 +154,16 @@ module.exports = function(app, middleware) {
var router = express.Router(), var router = express.Router(),
pageRouter = express.Router(), pageRouter = express.Router(),
pluginRouter = express.Router(), pluginRouter = express.Router(),
authRouter = express.Router(),
relativePath = nconf.get('relative_path'); relativePath = nconf.get('relative_path');
pluginRouter.render = function() { pluginRouter.render = function() {
app.render.apply(app, arguments); app.render.apply(app, arguments);
}; };
// Set-up for hotswapping (when NodeBB reloads)
pluginRouter.hotswapId = 'plugins'; pluginRouter.hotswapId = 'plugins';
authRouter.hotswapId = 'auth';
app.all(relativePath + '/api/?*', middleware.prepareAPI); app.all(relativePath + '/api/?*', middleware.prepareAPI);
app.all(relativePath + '/api/admin/*', middleware.admin.isAdmin, 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); apiRoutes(router, middleware, controllers);
feedRoutes(router, middleware, controllers); feedRoutes(router, middleware, controllers);
pluginRoutes(router, middleware, controllers); pluginRoutes(router, middleware, controllers);
authRoutes.createRoutes(router, middleware, controllers);
app.use(relativePath, express.static(path.join(__dirname, '../../', 'public'), { app.use(relativePath, express.static(path.join(__dirname, '../../', 'public'), {
maxAge: app.enabled('cache') ? 5184000000 : 0 maxAge: app.enabled('cache') ? 5184000000 : 0
@ -195,6 +198,7 @@ module.exports = function(app, middleware) {
app.use(relativePath, router); app.use(relativePath, router);
app.use(relativePath, pluginRouter); app.use(relativePath, pluginRouter);
app.use(relativePath, pageRouter); app.use(relativePath, pageRouter);
app.use(relativePath, authRouter);
if (process.env.NODE_ENV === 'development') { if (process.env.NODE_ENV === 'development') {
require('./debug')(app, middleware, controllers); require('./debug')(app, middleware, controllers);
@ -204,6 +208,7 @@ module.exports = function(app, middleware) {
// Add plugin routes // Add plugin routes
plugins.reloadRoutes(); plugins.reloadRoutes();
authRoutes.reloadRoutes();
}; };
function handleErrors(err, req, res, next) { function handleErrors(err, req, res, next) {

@ -37,7 +37,6 @@ if(nconf.get('ssl')) {
var port = nconf.get('PORT') || nconf.get('port'); var port = nconf.get('PORT') || nconf.get('port');
logger.init(app); logger.init(app);
auth.registerApp(app);
emailer.registerApp(app); emailer.registerApp(app);
if (cluster.isWorker && process.env.handle_jobs === 'true') { if (cluster.isWorker && process.env.handle_jobs === 'true') {
@ -64,6 +63,7 @@ if(nconf.get('ssl')) {
middleware = middleware(app, data); middleware = middleware(app, data);
routes(app, middleware); routes(app, middleware);
plugins.prepareApp(app, middleware); plugins.prepareApp(app, middleware);
auth.registerApp(app, middleware);
if (err) { if (err) {
winston.error('Errors were encountered while attempting to initialise NodeBB.'); winston.error('Errors were encountered while attempting to initialise NodeBB.');

Loading…
Cancel
Save