-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
create the OTP SDK with update event and getting refresh token from t…
…he api rel #94
- Loading branch information
Showing
6 changed files
with
94 additions
and
49 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'), | ||
}; |