Skip to content

Commit

Permalink
work on auth middleware, cookies, userdata for handlebars
Browse files Browse the repository at this point in the history
Relates #44 Closes #69
  • Loading branch information
tdoran committed May 11, 2018
1 parent 7645794 commit 8ad74bb
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 11 deletions.
16 changes: 8 additions & 8 deletions src/controllers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const passport = require('passport');
const home = require('./home');
const profileDetails = require('./profileDetails');
const error = require('./error');
const { ensureAuthenticated } = require('./middleware');
const { ensureAuthenticated, updateUserSession, protectedRoute } = require('./middleware');

// UNPROTECTED ROUTES //
router.get('/', home.get);
Expand All @@ -14,17 +14,15 @@ router.get('/notmember', (req, res) => {
});

// PROTECTED ROUTES //
router.get('/myprofile', ensureAuthenticated, (req, res) => {
router.get('/myprofile', updateUserSession, protectedRoute, (req, res) => {
res.send('profile');
});
router.get(
'/myprofile/mydetails/edit', ensureAuthenticated, profileDetails.get
);
router.get('/myprofile/:github_id/mydetails/edit', updateUserSession, protectedRoute, profileDetails.get);

// AUTHENTICATION ROUTES //
router.get(
'/auth/github/signup',
passport.authenticate('github', { scope: ['read:org'] })
passport.authenticate('github', { scope: ['read:org'] }),
);

router.get(
Expand All @@ -39,9 +37,11 @@ router.get(
if (info.message === 'Not FAC member') {
return res.redirect('/notmember');
} else if (info.message === 'Login successful') {
return res.redirect('/myprofile');
req.session.registeredProfile = true;
return res.redirect(`/myprofile/${req.user.github_id}/mydetails/edit`);
} else if (info.message === 'Signup successful') {
return res.redirect('/myprofile/mydetails/edit');
req.session.registeredProfile = false;
return res.redirect(`/myprofile/${req.user.github_id}/mydetails/edit`);
}
});
})(req, res, next);
Expand Down
39 changes: 38 additions & 1 deletion src/controllers/middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,41 @@ exports.ensureAuthenticated = (req, res, next) => {
return next();
}
res.redirect('/');
}
};

// MIDDLEWARE to ensure user is authenticated
exports.updateUserSession = (req, res, next) => {
let userInfo;
if (req.isAuthenticated()) {
userInfo = req.user;
if (req.session.registeredProfile) {
userInfo.session = {
login: true,
signup: false,
};
return next(null, userInfo);
}
userInfo.session = {
login: false,
signup: true,
};
return next(null, userInfo);
}

userInfo = {
session: {
login: false,
signup: false,
},
};
req.user = userInfo;
return next();
};
// denied - should redirect to login;

exports.protectedRoute = (req, res, next) => {
if (!req.user.session.login && !req.user.session.signup) {
return res.send('please login');
}
return next(null, req.user);
};
5 changes: 3 additions & 2 deletions src/controllers/profileDetails.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
exports.get = (req, res) => {
res.render('profileDetails', { activePage: { profile: true }, loggedIn: true });
};
const { user } = req;
res.render('profileDetails', { activePage: { profile: true }, user });
};

0 comments on commit 8ad74bb

Please sign in to comment.