Streamline our middleware into one file.

This commit is contained in:
David Humphrey (:humph) david.humphrey@senecacollege.ca 2014-07-22 13:17:01 -04:00
Родитель 5eae612a34
Коммит 805941fb6d
3 изменённых файлов: 31 добавлений и 30 удалений

Просмотреть файл

@ -1,4 +1,33 @@
var Sync = require('./lib/sync');
var env = require('./lib/environment');
var websocketAuth = require('./lib/websocket-auth');
function generateError( code, msg ) {
var err = new Error( msg );
err.status = code;
return err;
}
module.exports = {
authenticationHandler: function( req, res, next ) {
var username = req.session && req.session.user && req.session.user.username;
if ( !username ) {
return next( generateError( 401, "Webmaker Authentication Required." ) );
}
req.session.sessionId = websocketAuth.createSessionTracker(username, req.session.sessionId);
req.params.username = username;
req.params.sessionId = req.session.sessionId;
next();
},
crossOriginHandler: function( req, res, next ) {
res.header( "Access-Control-Allow-Origin", env.get("ALLOWED_CORS_DOMAINS") );
res.header( "Access-Control-Allow-Credentials", true );
next();
},
errorHandler: function( err, req, res, next ) {
if ( typeof err === "string" ) {
console.error( "String passed to next(), expected an Error object, got: %s", err );
@ -11,6 +40,7 @@ module.exports = {
res.status( error.status ).json( error );
},
fourOhFourHandler: function( req, res, next ) {
var error = {
message: "You found a loose thread!",

Просмотреть файл

@ -1,5 +1,5 @@
// Expose internals
var middleware = require( './middleware' ),
var middleware = require( '../middleware.js' ),
env = require('../lib/environment'),
version = require('../../package.json').version,
FilerWebServer = require('../lib/filer-www'),

Просмотреть файл

@ -1,29 +0,0 @@
var Sync = require('../../lib/sync');
var env = require('../../lib/environment');
var websocketAuth = require('../../lib/websocket-auth');
function generateError( code, msg ) {
var err = new Error( msg );
err.status = code;
return err;
}
module.exports = {
authenticationHandler: function( req, res, next ) {
var username = req.session && req.session.user && req.session.user.username;
if ( !username ) {
return next( generateError( 401, "Webmaker Authentication Required." ) );
}
req.session.sessionId = websocketAuth.createSessionTracker(username, req.session.sessionId);
req.params.username = username;
req.params.sessionId = req.session.sessionId;
next();
},
crossOriginHandler: function( req, res, next ) {
res.header( "Access-Control-Allow-Origin", env.get("ALLOWED_CORS_DOMAINS") );
res.header( "Access-Control-Allow-Credentials", true );
next();
}
};