Add duplicate route functions to separate file

main
sudoer777 2021-11-26 15:50:43 -07:00
parent dd0a9c4316
commit fb414ab9d8
4 changed files with 40 additions and 63 deletions

View File

@ -1,20 +1,8 @@
var express = require('express'); var express = require('express');
var router = express.Router(); var router = express.Router();
const passport = require('passport'); const passport = require('passport');
const accounts = require('./../database/accounts/accounts');
const app = require('../app'); const app = require('../app');
function adminLoggedIn(req, res, next) {
if (req.user && req.user[2]) {
next();
}
else {
req.flash('error', 'An admin account is required to access this page.');
res.redirect('/auth/login');
}
}
router.get('/login', (req, res, next) => { router.get('/login', (req, res, next) => {
res.render('accounts/login', { title : "Login", message: req.flash('error') }); res.render('accounts/login', { title : "Login", message: req.flash('error') });
}); });

View File

@ -0,0 +1,21 @@
function adminLoggedIn(req, res, next) {
if (req.user && req.user[2]) {
next();
}
else {
req.flash('error', 'An admin account is required to access this page.');
res.redirect('/auth/login');
}
}
function userLoggedIn(req, res, next) {
if (req.user) {
next();
}
else {
res.redirect('/auth/login');
}
}
exports.admin = adminLoggedIn;
exports.user = userLoggedIn;

View File

@ -1,5 +1,6 @@
var express = require('express'); var express = require('express');
var router = express.Router(); var router = express.Router();
var sports = require('../database/scores/sports'); var sports = require('../database/scores/sports');
var seasons = require('../database/scores/seasons'); var seasons = require('../database/scores/seasons');
var genders = require('../database/scores/genders'); var genders = require('../database/scores/genders');
@ -8,24 +9,7 @@ var teams = require('../database/scores/teams');
var games = require('../database/scores/games'); var games = require('../database/scores/games');
var accounts = require('../database/accounts/accounts'); var accounts = require('../database/accounts/accounts');
function adminLoggedIn(req, res, next) { var checkLoginStatus = require('./checkLoginStatus');
if (req.user && req.user[2]) {
next();
}
else {
req.flash('error', 'An admin account is required to access this page.');
res.redirect('/auth/login');
}
}
function userLoggedIn(req, res, next) {
if (req.user) {
next();
}
else {
res.redirect('/auth/login');
}
}
router.get('/sports', function(req, res, next) { router.get('/sports', function(req, res, next) {
sports.retrieveAll() sports.retrieveAll()
@ -81,12 +65,12 @@ router.get('/game', function(req, res, next) {
.then(data => res.json(data)); .then(data => res.json(data));
}) })
router.get('/accounts', adminLoggedIn, function(req, res, next) { router.get('/accounts', checkLoginStatus.admin, function(req, res, next) {
accounts.retrieveAll() accounts.retrieveAll()
.then(data => res.json(data)); .then(data => res.json(data));
}) })
router.get('/account', userLoggedIn, function(req, res, next) { router.get('/account', checkLoginStatus.user, function(req, res, next) {
const userIsAdmin = req.user[2]; const userIsAdmin = req.user[2];
const loggedInAccountID = req.user[0]; const loggedInAccountID = req.user[0];
const requestedAccountID = req.query.account; const requestedAccountID = req.query.account;

View File

@ -1,5 +1,6 @@
var express = require('express'); var express = require('express');
var router = express.Router(); var router = express.Router();
var genders = require('../database/scores/genders'); var genders = require('../database/scores/genders');
var games = require('../database/scores/games'); var games = require('../database/scores/games');
var seasons = require('../database/scores/seasons'); var seasons = require('../database/scores/seasons');
@ -9,38 +10,21 @@ var genders = require('../database/scores/genders');
var teams = require('../database/scores/teams'); var teams = require('../database/scores/teams');
var accounts = require('../database/accounts/accounts'); var accounts = require('../database/accounts/accounts');
function userLoggedIn(req, res, next) { var checkLoginStatus = require('./checkLoginStatus');
if (req.user) {
next();
}
else {
res.redirect('/auth/login');
}
}
function adminLoggedIn(req, res, next) {
if (req.user && req.user[2]) {
next();
}
else {
req.flash('error', 'An admin account is required to access this page.');
res.redirect('/auth/login');
}
}
router.get('/' ,userLoggedIn, function(req, res, next) { router.get('/' ,checkLoginStatus.user, function(req, res, next) {
if(req.user[2]) res.render('manage', { title: 'Score Management', userLoggedIn: !!req.user }); if(req.user[2]) res.render('manage', { title: 'Score Management', userLoggedIn: !!req.user });
else res.render('manage/manage-nonadmin', { title: "My Games", userLoggedIn: !!req.user }); else res.render('manage/manage-nonadmin', { title: "My Games", userLoggedIn: !!req.user });
}); });
router.get('/game', userLoggedIn, function(req, res, next) { router.get('/game', checkLoginStatus.user, function(req, res, next) {
let title = req.query.game ? 'Edit Game' : 'Submit Score' let title = req.query.game ? 'Edit Game' : 'Submit Score'
res.render('manage/addgame', { title, userLoggedIn: !!req.user }); res.render('manage/addgame', { title, userLoggedIn: !!req.user });
}); });
router.post('/game', userLoggedIn, function(req, res, next) { router.post('/game', checkLoginStatus.user, function(req, res, next) {
const seasonID = req.body['year']; const seasonID = req.body['year'];
const sportID = req.body['sport']; const sportID = req.body['sport'];
const gender = (req.body['gender'] == "female") ? genders.FEMALE : genders.MALE; const gender = (req.body['gender'] == "female") ? genders.FEMALE : genders.MALE;
@ -72,11 +56,11 @@ router.post('/game', userLoggedIn, function(req, res, next) {
}); });
}); });
router.get('/season', adminLoggedIn, function(req, res, next) { router.get('/season', checkLoginStatus.admin, function(req, res, next) {
res.render('manage/addseason', { title: 'Add Season', currentYear : (new Date()).getFullYear(), userLoggedIn: !!req.user }); res.render('manage/addseason', { title: 'Add Season', currentYear : (new Date()).getFullYear(), userLoggedIn: !!req.user });
}); });
router.post('/season', adminLoggedIn, function(req, res, next) { router.post('/season', checkLoginStatus.admin, function(req, res, next) {
const year = req.body['year']; const year = req.body['year'];
const seasonID = req.body['season']; const seasonID = req.body['season'];
@ -86,11 +70,11 @@ router.post('/season', adminLoggedIn, function(req, res, next) {
else seasons.add(year).then(res.redirect("/manage")); else seasons.add(year).then(res.redirect("/manage"));
}); });
router.get('/sport', adminLoggedIn, function(req, res, next) { router.get('/sport', checkLoginStatus.admin, function(req, res, next) {
res.render('manage/addsport', { title: 'Add Sport', userLoggedIn: !!req.user }); res.render('manage/addsport', { title: 'Add Sport', userLoggedIn: !!req.user });
}); });
router.post('/sport', adminLoggedIn, function(req, res, next) { router.post('/sport', checkLoginStatus.admin, function(req, res, next) {
const name = req.body['name']; const name = req.body['name'];
const id = req.body['sport']; const id = req.body['sport'];
const remove = req.body['remove']; const remove = req.body['remove'];
@ -100,13 +84,13 @@ router.post('/sport', adminLoggedIn, function(req, res, next) {
else sports.add(name).then(res.redirect('/manage')); else sports.add(name).then(res.redirect('/manage'));
}); });
router.get('/division', adminLoggedIn, function(req, res, next) { router.get('/division', checkLoginStatus.admin, function(req, res, next) {
let title = req.query.division ? 'Edit Division' : 'Add Division' let title = req.query.division ? 'Edit Division' : 'Add Division'
res.render('manage/adddivision', { title, userLoggedIn: !!req.user }); res.render('manage/adddivision', { title, userLoggedIn: !!req.user });
}); });
router.post('/division', adminLoggedIn, function(req, res, next) { router.post('/division', checkLoginStatus.admin, function(req, res, next) {
const name = req.body['name']; const name = req.body['name'];
const sport = req.body['sport']; const sport = req.body['sport'];
const genderName = req.body['gender']; const genderName = req.body['gender'];
@ -131,13 +115,13 @@ router.post('/division', adminLoggedIn, function(req, res, next) {
} }
}); });
router.get('/team', adminLoggedIn, function(req, res, next) { router.get('/team', checkLoginStatus.admin, function(req, res, next) {
let title = req.query.team ? 'Edit Team' : 'Add Team' let title = req.query.team ? 'Edit Team' : 'Add Team'
res.render('manage/addteam', { title, userLoggedIn: !!req.user }); res.render('manage/addteam', { title, userLoggedIn: !!req.user });
}); });
router.post('/team', adminLoggedIn, function(req, res, next) { router.post('/team', checkLoginStatus.admin, function(req, res, next) {
const name = req.body['name']; const name = req.body['name'];
const sport = req.body['sport']; const sport = req.body['sport'];
@ -149,7 +133,7 @@ router.post('/team', adminLoggedIn, function(req, res, next) {
else teams.add(name, sport).then(res.redirect("/manage")); else teams.add(name, sport).then(res.redirect("/manage"));
}); });
router.get('/account', userLoggedIn, (req, res, next) => { router.get('/account', checkLoginStatus.user, (req, res, next) => {
const userIsAdmin = req.user[2]; const userIsAdmin = req.user[2];
const accountID = req.user[0]; const accountID = req.user[0];
@ -165,7 +149,7 @@ router.get('/account', userLoggedIn, (req, res, next) => {
} }
}); });
router.post('/account', userLoggedIn, async function(req, res, next) { router.post('/account', checkLoginStatus.user, async function(req, res, next) {
const email = req.body.email; const email = req.body.email;
const password = req.body.password; const password = req.body.password;