2016-05-05 23:32:31 -06:00
|
|
|
secret = require('./secrets.js');
|
|
|
|
|
2016-03-31 15:57:34 -06:00
|
|
|
module.exports = {
|
|
|
|
|
|
|
|
throwErr: function(req,err){
|
2016-04-27 15:14:32 -06:00
|
|
|
console.log('Middleware error:'+err+'\nfor request:\n'+req);
|
2016-05-05 23:32:31 -06:00
|
|
|
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
|
|
|
},
|
|
|
|
|
|
|
|
ensureAuth: function(req,res,next){
|
|
|
|
if (req.isAuthenticated()) { return next(); }
|
|
|
|
else {
|
|
|
|
req.session.returnTo = req.path;
|
2016-05-12 20:43:01 -06:00
|
|
|
console.log('mw.ensureAuth: Going to redirect to '+req.path+' after login.'); // TODO: Correct next path
|
2016-03-31 15:57:34 -06:00
|
|
|
req.flash('error', 'You must be signed in to do that. <a href="/login">Click here to log in</a>. ');
|
|
|
|
res.redirect('/');
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
ensureAdmin: function(req,res,next){
|
|
|
|
if (req.user.isAdmin) { return next(); }
|
|
|
|
else { res.sendStatus(401); }
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|