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-13 16:53:18 -06:00
|
|
|
throwErr: (err,req=null)=>{
|
|
|
|
console.error(`⛔️ ${err.stack}`);
|
|
|
|
if (req){
|
|
|
|
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.message);
|
|
|
|
}
|
2017-04-01 11:03:05 -06:00
|
|
|
}
|
|
|
|
},
|
2017-04-13 16:53:18 -06:00
|
|
|
|
2017-04-01 11:03:05 -06:00
|
|
|
// Capitalize the first letter of a string
|
2017-04-13 16:53:18 -06:00
|
|
|
capitalize: (str)=>{
|
2017-04-01 11:03:05 -06:00
|
|
|
return str.charAt(0).toUpperCase() + str.slice(1);
|
|
|
|
},
|
2017-04-13 16:53:18 -06:00
|
|
|
|
2017-04-01 11:03:05 -06:00
|
|
|
// Ensure authentication
|
2017-04-13 16:53:18 -06:00
|
|
|
ensureAuth: (req,res,next)=>{
|
2017-04-01 11:03:05 -06:00
|
|
|
if (req.isAuthenticated()) { return next(); }
|
|
|
|
else { res.redirect('/login'); }
|
|
|
|
},
|
2017-04-13 16:53:18 -06:00
|
|
|
|
2017-04-01 11:03:05 -06:00
|
|
|
// Ensure administrator
|
2017-04-13 16:53:18 -06:00
|
|
|
ensureAdmin: (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
|
|
|
};
|