2017-03-15 02:01:55 -06:00
|
|
|
'use strict';
|
|
|
|
|
2017-04-01 11:03:05 -06:00
|
|
|
const env = require('./env.js');
|
2016-05-05 23:32:31 -06:00
|
|
|
|
2017-04-01 11:03:05 -06:00
|
|
|
module.exports = {
|
2016-06-12 18:18:34 -06:00
|
|
|
|
2017-04-01 11:03:05 -06:00
|
|
|
// Throw error
|
2017-04-12 11:41:27 -06:00
|
|
|
throwErr: function(err,req=null){
|
2017-04-11 19:37:14 -06:00
|
|
|
console.error('Middleware error:'+err.message+'\nfor request:\n'+req);
|
2017-04-01 11:03:05 -06:00
|
|
|
if (env.mode==='production') {
|
|
|
|
req.flash('danger', 'An error occured. <br>Would you like to <a href="https://github.com/Tracman-org/Server/issues/new">report it</a>?');
|
|
|
|
} else { // development
|
|
|
|
req.flash('danger', err);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
// Capitalize the first letter of a string
|
|
|
|
capitalize: function(str){ 'use strict';
|
|
|
|
return str.charAt(0).toUpperCase() + str.slice(1);
|
|
|
|
},
|
|
|
|
|
|
|
|
// Ensure authentication
|
|
|
|
ensureAuth: function(req,res,next){
|
|
|
|
if (req.isAuthenticated()) { return next(); }
|
|
|
|
else { res.redirect('/login'); }
|
|
|
|
},
|
|
|
|
|
|
|
|
// Ensure administrator
|
|
|
|
ensureAdmin: function(req,res,next){
|
2016-06-30 14:40:21 -06:00
|
|
|
if (req.user.isAdmin){ return next(); }
|
2017-04-01 11:03:05 -06:00
|
|
|
else { res.sendStatus(401); }
|
2016-07-01 19:14:36 -06:00
|
|
|
//TODO: test this by logging in as !isAdmin and go to /admin
|
2017-04-01 11:03:05 -06:00
|
|
|
}
|
2016-06-12 18:18:34 -06:00
|
|
|
|
2016-03-31 15:57:34 -06:00
|
|
|
};
|