Skip to content

Commit

Permalink
Added Home Router with middleware function
Browse files Browse the repository at this point in the history
  • Loading branch information
itajenglish committed Feb 27, 2017
1 parent 6b30f67 commit ae3b2ce
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 8 deletions.
6 changes: 2 additions & 4 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand All @@ -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;
Expand Down
11 changes: 7 additions & 4 deletions controllers/Home.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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;
13 changes: 13 additions & 0 deletions lib/helpers/checkSession.js
Original file line number Diff line number Diff line change
@@ -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();
}
}
Empty file removed lib/helpers/validateUser.js
Empty file.

0 comments on commit ae3b2ce

Please sign in to comment.