tracman-server/config/middleware.js

37 lines
890 B
JavaScript
Raw Normal View History

'use strict';
2017-04-01 11:03:05 -06:00
const env = require('./env.js');
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
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-01 11:03:05 -06:00
// Capitalize the first letter of a string
capitalize: (str)=>{
2017-04-01 11:03:05 -06:00
return str.charAt(0).toUpperCase() + str.slice(1);
},
2017-04-01 11:03:05 -06:00
// Ensure authentication
ensureAuth: (req,res,next)=>{
2017-04-01 11:03:05 -06:00
if (req.isAuthenticated()) { return next(); }
else { res.redirect('/login'); }
},
2017-04-01 11:03:05 -06:00
// Ensure administrator
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
};