Skip to content

Commit

Permalink
implement the refresh token functionality
Browse files Browse the repository at this point in the history
rel #94
  • Loading branch information
Karyum committed Feb 5, 2018
1 parent ef86b99 commit cc22367
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 3 deletions.
15 changes: 12 additions & 3 deletions src/controllers/events/modify_content.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const Request = require('request');
const { eventsURL } = require('../../constants/urls.json');
const checkCookie = require('../../helpers/check_cookie.js');
const getRefreshToken = require('../../helpers/get_refresh_token.js');

module.exports = (req, res) => {
const apiBody = {
Expand Down Expand Up @@ -61,9 +62,17 @@ module.exports = (req, res) => {
if (error) {
return res.status(500).send(res.locals.localText.serverError);
} else if (apiResponse.statusCode !== correctResponseStatusCode) {
return res
.status(apiResponseBody.statusCode)
.send(apiResponseBody.message);
console.log('im here lad');
console.log(apiResponseBody);
if (apiResponseBody.error === 'Unauthorized') {
getRefreshToken(req, res)
.then(() => res.status(400).send('Try again'))
.catch(() => res.status(500).send('Server error! try again'));
} else {
return res
.status(apiResponseBody.statusCode)
.send(apiResponseBody.message);
}
} else {
res.end(
JSON.stringify({ redirectUrl: `/${req.params.lang}/${urlEndpoint}` }),
Expand Down
52 changes: 52 additions & 0 deletions src/helpers/get_refresh_token.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
const verifyToken = require('./verify_token.js');
const queryString = require('query-string');
const Request = require('request');
const jwt = require('jsonwebtoken');
const { oauthTokenBaseURL } = require('../constants/urls.json');

module.exports = (req, res) => {
const requestToken = token => {
const tokenQueries = {
grant_type: 'refresh_token',
refresh_token: token.refresh_token,
client_id: process.env.CLIENT_ID,
client_secret: process.env.CLIENT_SECRET,
redirect_uri: process.env.REDIRECT_URI,
};

const options = {
method: 'POST',
uri: oauthTokenBaseURL,
body: queryString.stringify(tokenQueries),
headers: {
'content-type': 'application/x-www-form-urlencoded',
},
};
return new Promise((resolve, reject) => {
Request(options, (error, responseToken, body) => {
console.log('refresh status code', responseToken.statusCode);
console.log(body);
if (error || responseToken.statusCode !== 200) {
reject(error);
} else {
const { access_token, refresh_token } = JSON.parse(body);
const token = jwt.sign(
{ access_token, refresh_token },
process.env.JWT_SECRET,
);
console.log('TOKEN', token);
res.clearCookie('access');
res.cookie('access', token, { maxAge: 604800000 });
resolve();
}
});
});
};

return new Promise((resolve, reject) => {
verifyToken(req)
.then(requestToken)
.then(() => resolve())
.catch(() => reject());
});
};
16 changes: 16 additions & 0 deletions src/helpers/verify_token.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const jwt = require('jsonwebtoken');

module.exports = req => {
const { access } = req.cookies && req.cookies;

return new Promise((resolve, reject) => {
jwt.verify(access, process.env.JWT_SECRET, (error, decoded) => {
if (error) {
console.log('holy hell', error);
return reject(error);
} else {
return resolve(decoded);
}
});
});
};

0 comments on commit cc22367

Please sign in to comment.