diff --git a/src/controllers/OAuth/token.js b/src/controllers/OAuth/token.js index 772d940..ddfe097 100644 --- a/src/controllers/OAuth/token.js +++ b/src/controllers/OAuth/token.js @@ -11,7 +11,7 @@ module.exports = (req, res) => { client_id: process.env.CLIENT_ID, client_secret: process.env.CLIENT_SECRET, redirect_uri: process.env.REDIRECT_URI, - grant_type: 'authorization_code' + grant_type: 'authorization_code', }; const options = { @@ -19,26 +19,30 @@ module.exports = (req, res) => { uri: oauthTokenBaseURL, body: queryString.stringify(tokenQueries), headers: { - 'content-type': 'application/x-www-form-urlencoded' - } + 'content-type': 'application/x-www-form-urlencoded', + }, }; if (req.query.state !== process.env.STATE) { return res.redirect('error', { statusCode: 400, - errorMessage: 'Something went wrong with your login information! please try again.' + errorMessage: + 'Something went wrong with your login information! please try again.', }); } else { Request(options, (error, responseToken, body) => { if (error || responseToken.statusCode !== 200) { return res.redirect('error', { statusCode: 500, - errorMessage: 'Server error!' + errorMessage: 'Server error!', }); } else { - const parsedBody = JSON.parse(body); - const token = jwt.sign(parsedBody.access_token, process.env.JWT_SECRET); - res.cookie('token', token, {maxAge: 604800000}); + const { access_token, refresh_token } = JSON.parse(body); + const token = jwt.sign( + { access_token, refresh_token }, + process.env.JWT_SECRET, + ); + res.cookie('access', token, { maxAge: 604800000 }); if (req.cookies && req.cookies.referredUrl) { res.redirect(req.cookies.referredUrl); diff --git a/src/controllers/events/delete_event.js b/src/controllers/events/delete_event.js index 8fca2fe..9749415 100644 --- a/src/controllers/events/delete_event.js +++ b/src/controllers/events/delete_event.js @@ -3,43 +3,47 @@ const { eventsURL } = require('../../constants/urls.json'); const jwt = require('jsonwebtoken'); module.exports = (req, res) => { - if (req.cookies && req.cookies.token) { - jwt.verify(req.cookies.token, process.env.JWT_SECRET, (error, decodedToken) => { - if (error) { - return res.render('error', { - statusCode: 500, - errorMessage: res.locals.localText.serverError - }); - } else { - const reqOptions = { - url: `${eventsURL}/${req.params.id}`, - method: 'DELETE', - auth: { - 'bearer': decodedToken - } - }; - Request(reqOptions, (error, response) => { - if (error) { - return res.render('error', { - statusCode: 500, - errorMessage: res.locals.localText.serverError - }); - } - if (response.statusCode !== 204) { - return res.render('error', { - statusCode: 400, - errorMessage: res.locals.localText.badRequest - }); - } else { - res.redirect(`/${req.params.lang}/events`); - } - }); - } - }); + if (req.cookies && req.cookies.access) { + jwt.verify( + req.cookies.access, + process.env.JWT_SECRET, + (error, decodedToken) => { + if (error) { + return res.render('error', { + statusCode: 500, + errorMessage: res.locals.localText.serverError, + }); + } else { + const reqOptions = { + url: `${eventsURL}/${req.params.id}`, + method: 'DELETE', + auth: { + bearer: decodedToken, + }, + }; + Request(reqOptions, (error, response) => { + if (error) { + return res.render('error', { + statusCode: 500, + errorMessage: res.locals.localText.serverError, + }); + } + if (response.statusCode !== 204) { + return res.render('error', { + statusCode: 400, + errorMessage: res.locals.localText.badRequest, + }); + } else { + res.redirect(`/${req.params.lang}/events`); + } + }); + } + }, + ); } else { return res.redirect('error', { statusCode: 500, - errorMessage: res.locals.localText.serverError + errorMessage: res.locals.localText.serverError, }); } }; diff --git a/src/helpers/check_cookie.js b/src/helpers/check_cookie.js index de69ca7..df9f0fe 100644 --- a/src/helpers/check_cookie.js +++ b/src/helpers/check_cookie.js @@ -1,11 +1,11 @@ const jwt = require('jsonwebtoken'); const checkCookie = (req, cb) => { - if (req.cookies && req.cookies.token) { - jwt.verify(req.cookies.token, process.env.JWT_SECRET, (error, decoded) => { + if (req.cookies && req.cookies.access) { + jwt.verify(req.cookies.access, process.env.JWT_SECRET, (error, decoded) => { if (error) cb(error); else { - cb(null, decoded); + cb(null, decoded.access_token); } }); } else { diff --git a/src/middleware/checkLoggedIn.js b/src/middleware/checkLoggedIn.js index 3f8b766..6e7062a 100644 --- a/src/middleware/checkLoggedIn.js +++ b/src/middleware/checkLoggedIn.js @@ -3,20 +3,18 @@ // referredUrl to know where to go back to after looged in! const jwt = require('jsonwebtoken'); +const verifyToken = require('../helpers/verify_token.js'); module.exports = (req, res, next) => { - const token = req.cookies && req.cookies.token; + const access = req.cookies && req.cookies.access; - if (token) { - jwt.verify(token, process.env.JWT_SECRET, (error) => { - if (error) { - return res.redirect(`/${req.params.lang}/login`); - } else { - return next(); - } - }); + if (access) { + console.log('worked ?'); + verifyToken(req) + .then(() => next()) + .catch(err => res.redirect(`/${req.params.lang}/login`)); } else { - res.cookie('referredUrl', req.url, {maxAge: 300000}); + res.cookie('referredUrl', req.url, { maxAge: 300000 }); return res.redirect(`/${req.params.lang}/login`); } };