From fb414ab9d8c424cb20eb387e36987aad41d43686 Mon Sep 17 00:00:00 2001 From: sudoer777 <78781902+sudoer777@users.noreply.github.com> Date: Fri, 26 Nov 2021 15:50:43 -0700 Subject: [PATCH] Add duplicate route functions to separate file --- routes/auth.js | 12 ---------- routes/checkLoginStatus.js | 21 +++++++++++++++++ routes/data.js | 24 ++++---------------- routes/manage.js | 46 +++++++++++++------------------------- 4 files changed, 40 insertions(+), 63 deletions(-) create mode 100644 routes/checkLoginStatus.js diff --git a/routes/auth.js b/routes/auth.js index 25159ab..1e1940b 100644 --- a/routes/auth.js +++ b/routes/auth.js @@ -1,20 +1,8 @@ var express = require('express'); var router = express.Router(); const passport = require('passport'); -const accounts = require('./../database/accounts/accounts'); 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) => { res.render('accounts/login', { title : "Login", message: req.flash('error') }); }); diff --git a/routes/checkLoginStatus.js b/routes/checkLoginStatus.js new file mode 100644 index 0000000..d9fd970 --- /dev/null +++ b/routes/checkLoginStatus.js @@ -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; \ No newline at end of file diff --git a/routes/data.js b/routes/data.js index c875622..d241947 100644 --- a/routes/data.js +++ b/routes/data.js @@ -1,5 +1,6 @@ var express = require('express'); var router = express.Router(); + var sports = require('../database/scores/sports'); var seasons = require('../database/scores/seasons'); var genders = require('../database/scores/genders'); @@ -8,24 +9,7 @@ var teams = require('../database/scores/teams'); var games = require('../database/scores/games'); var accounts = require('../database/accounts/accounts'); -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'); - } -} +var checkLoginStatus = require('./checkLoginStatus'); router.get('/sports', function(req, res, next) { sports.retrieveAll() @@ -81,12 +65,12 @@ router.get('/game', function(req, res, next) { .then(data => res.json(data)); }) -router.get('/accounts', adminLoggedIn, function(req, res, next) { +router.get('/accounts', checkLoginStatus.admin, function(req, res, next) { accounts.retrieveAll() .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 loggedInAccountID = req.user[0]; const requestedAccountID = req.query.account; diff --git a/routes/manage.js b/routes/manage.js index c995742..506a5f1 100644 --- a/routes/manage.js +++ b/routes/manage.js @@ -1,5 +1,6 @@ var express = require('express'); var router = express.Router(); + var genders = require('../database/scores/genders'); var games = require('../database/scores/games'); var seasons = require('../database/scores/seasons'); @@ -9,38 +10,21 @@ var genders = require('../database/scores/genders'); var teams = require('../database/scores/teams'); var accounts = require('../database/accounts/accounts'); -function userLoggedIn(req, res, next) { - 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'); - } -} +var checkLoginStatus = require('./checkLoginStatus'); -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 }); 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' 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 sportID = req.body['sport']; 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 }); }); -router.post('/season', adminLoggedIn, function(req, res, next) { +router.post('/season', checkLoginStatus.admin, function(req, res, next) { const year = req.body['year']; 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")); }); -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 }); }); -router.post('/sport', adminLoggedIn, function(req, res, next) { +router.post('/sport', checkLoginStatus.admin, function(req, res, next) { const name = req.body['name']; const id = req.body['sport']; 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')); }); -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' 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 sport = req.body['sport']; 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' 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 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")); }); -router.get('/account', userLoggedIn, (req, res, next) => { +router.get('/account', checkLoginStatus.user, (req, res, next) => { const userIsAdmin = req.user[2]; 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 password = req.body.password;