Skip to content

Commit

Permalink
change the name of the cookie
Browse files Browse the repository at this point in the history
rel #94
  • Loading branch information
Karyum committed Feb 5, 2018
1 parent d80e4a2 commit ef86b99
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 55 deletions.
20 changes: 12 additions & 8 deletions src/controllers/OAuth/token.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,34 +11,38 @@ 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 = {
method: 'POST',
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);
Expand Down
72 changes: 38 additions & 34 deletions src/controllers/events/delete_event.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
});
}
};
6 changes: 3 additions & 3 deletions src/helpers/check_cookie.js
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down
18 changes: 8 additions & 10 deletions src/middleware/checkLoggedIn.js
Original file line number Diff line number Diff line change
Expand Up @@ -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`);
}
};

0 comments on commit ef86b99

Please sign in to comment.