2016-05-05 23:32:31 -06:00
|
|
|
secret = require('./secrets.js');
|
|
|
|
|
2016-06-12 18:18:34 -06:00
|
|
|
var throwErr = function(req,err){
|
|
|
|
console.log('Middleware error:'+err+'\nfor request:\n'+req);
|
|
|
|
if (secret.env==='production') {
|
|
|
|
req.flash('error', 'An error occured. <br>Would you like to <a href="/bug">report it</a>?');
|
|
|
|
req.flash('error-message',err);
|
|
|
|
} else {
|
|
|
|
req.flash('error',err);
|
|
|
|
req.flash('error-message',err);
|
|
|
|
}
|
|
|
|
};
|
2016-03-31 15:57:34 -06:00
|
|
|
|
2016-06-12 18:18:34 -06:00
|
|
|
var ensureAuth = function(req,res,next){
|
|
|
|
if (req.isAuthenticated()) { return next(); }
|
|
|
|
else {
|
|
|
|
// req.session.returnTo = req.path;
|
|
|
|
// console.log('mw.ensureAuth: Going to redirect to '+req.path+' after login.'); // TODO: Correct next path
|
|
|
|
req.flash('error', 'You must be signed in to do that. <a href="/login">Click here to log in</a>. ');
|
|
|
|
res.redirect(req.path);
|
2016-03-31 15:57:34 -06:00
|
|
|
}
|
2016-06-12 18:18:34 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
var ensureAdmin = function(req,res,next){
|
|
|
|
if (req.isAuthenticated()&&req.user.isAdmin) { return next(); }
|
|
|
|
else { res.sendStatus(401); }
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
throwErr,
|
|
|
|
ensureAuth,
|
|
|
|
ensureAdmin
|
2016-03-31 15:57:34 -06:00
|
|
|
};
|