diff --git a/src/controllers/index.js b/src/controllers/index.js index 9d10128..11cfd4e 100644 --- a/src/controllers/index.js +++ b/src/controllers/index.js @@ -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); @@ -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( @@ -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); diff --git a/src/controllers/middleware.js b/src/controllers/middleware.js index 9d5ad5d..edfaccd 100644 --- a/src/controllers/middleware.js +++ b/src/controllers/middleware.js @@ -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); +}; diff --git a/src/controllers/profileDetails.js b/src/controllers/profileDetails.js index c7d461e..05cb675 100644 --- a/src/controllers/profileDetails.js +++ b/src/controllers/profileDetails.js @@ -1,3 +1,4 @@ exports.get = (req, res) => { - res.render('profileDetails', { activePage: { profile: true }, loggedIn: true }); -}; \ No newline at end of file + const { user } = req; + res.render('profileDetails', { activePage: { profile: true }, user }); +};