Skip to content

Commit

Permalink
create the OTP SDK with update event and getting refresh token from t…
Browse files Browse the repository at this point in the history
…he api

rel #94
  • Loading branch information
Karyum committed Feb 14, 2018
1 parent b09dbd2 commit 26ebe81
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 49 deletions.
27 changes: 27 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"homepage": "https://github.com/foundersandcoders/OTP-Data-Entry#readme",
"dependencies": {
"aws-sdk": "^2.186.0",
"axios": "^0.17.1",
"body-parser": "^1.18.0",
"compression": "^1.7.1",
"cookie-parser": "^1.4.3",
Expand Down
49 changes: 0 additions & 49 deletions src/helpers/get_refresh_token.js

This file was deleted.

14 changes: 14 additions & 0 deletions src/otp_sdk/events.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const axios = require('axios');

module.exports.modify = (options, tools) => {
return new Promise(async (resolve, reject) => {
try {
await axios(options);
resolve();
} catch (error) {
error.response.data.error === 'Unauthorized'
? reject({ Unauthorized: true })
: reject();
}
});
};
47 changes: 47 additions & 0 deletions src/otp_sdk/get_refresh_token.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
const verifyToken = require('../helpers/verify_token.js');
const axios = require('axios');
const qs = require('querystring');
const jwt = require('jsonwebtoken');
const { oauthTokenBaseURL } = require('../constants/urls.json');

module.exports = (req, res) => {
const requestToken = token => {
console.log(1);
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',
baseURL: oauthTokenBaseURL,
data: qs.stringify(tokenQueries),
};

return new Promise(async (resolve, reject) => {
try {
const apiTokenResponse = await axios(options);
const { access_token, refresh_token } = apiTokenResponse.data;
const token = jwt.sign(
{ access_token, refresh_token },
process.env.JWT_SECRET,
);
res.clearCookie('access');
res.cookie('access', token, { maxAge: 604800000 });
resolve(access_token);
} catch (e) {
reject(e);
}
});
};

return new Promise(async (resolve, reject) => {
verifyToken(req)
.then(requestToken)
.then(token => resolve(token))
.catch(reject);
});
};
5 changes: 5 additions & 0 deletions src/otp_sdk/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports.events = require('./events.js');

module.exports.auth = {
getRefreshToken: require('./get_refresh_token.js'),
};

0 comments on commit 26ebe81

Please sign in to comment.