diff --git a/app.js b/app.js index a4df5b0..5a46c44 100644 --- a/app.js +++ b/app.js @@ -15,6 +15,7 @@ const USER_ROUTER = require('./controllers/Users'); const SESSION_ROUTER = require('./controllers/Sessions'); const HOME_ROUTER = require('./controllers/Home'); +// const checkSession = require('./lib/helpers/checkSession'); //configure express and related packages app.engine('html', mustache()); @@ -38,10 +39,7 @@ app.use(session({ } })); -app.all('/', (req, res, next) => { - console.log('helloWorld'); - next(); -}) +// app.all(['/'], checkSession) //start the server var PORT = process.env.PORT || 3000; diff --git a/controllers/Home.js b/controllers/Home.js index 596151a..080bd1e 100644 --- a/controllers/Home.js +++ b/controllers/Home.js @@ -2,10 +2,8 @@ const express = require('express'); const router = express.Router(); const { getAllDjs } = require('../models/Home'); -// Home#Show -router.get('/', getAllDjs, (req, res, next) => { +router.all('/', (req, res, next) => { const user = req.session.user; - if (user) { if (user.type === "fan") { console.log(user.type); @@ -14,9 +12,14 @@ router.get('/', getAllDjs, (req, res, next) => { res.redirect("/djboard"); } } else { + next(); + } +}) + +// Home#Show +router.get('/', getAllDjs, (req, res, next) => { const djs = req.user; res.render('home/index', { djs }); - } }); module.exports = router; diff --git a/lib/helpers/checkSession.js b/lib/helpers/checkSession.js new file mode 100644 index 0000000..7100905 --- /dev/null +++ b/lib/helpers/checkSession.js @@ -0,0 +1,13 @@ +module.exports = (req, res, next) => { + const user = req.session.user; + if (user) { + if (user.type === "fan") { + console.log(user.type); + res.redirect('/userboard'); + } else if (user.type === "dj") { + res.redirect("/djboard"); + } + } else { + next(); + } +} diff --git a/lib/helpers/validateUser.js b/lib/helpers/validateUser.js deleted file mode 100644 index e69de29..0000000