-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add a refresh token request #101
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Karyum the user should not click 'try again'.
This should all happen under the hood.
here is some very rough code that may help you understand the route to a solution. There are plent of ways of doing this.
const manageRequestAuth = makeRequest => {
makeRequest().
then(res => {
if (needsRefreshToken(res) {
return getRefreshToken().then(makeRequest)
} else return res
}
}
I think what you want to do here is create a file otp_sdk
then we go
OTP.events.get()
... and the same everywhere else
This way we can add the auth / refresh stuff to all the otp requests without turning everything into a spaghetti mess
so what we need here is something or an object that holds all the functions that make requests to the API correct ? so if i wanted to request the refresh token in other places in the code i would just require the |
@Karyum fair enough about keeping it out of this pr.
//otp-sdk.js
module.exports = {
createUser: withAuth(createUser)
} |
@des-des OTP.events
.modify(reqOptions, tools)
.then(res => res.end(res.redirectUrl))
.catch(err => {
if (err.Unauthorized) {
getRefreshToken(req, res)
.then(res =>
OTP.events
.modify(reqOptions, tools)
.then(res => res.end(res.redirectUrl))
.catch(error => res.status(500).send(error.message)),
)
.catch(err => console.log(err));
} else {
res.status(500).send(error.message);
}
}); Should i just use async/await ?? try {
const apiResponse = await OTP.events.modify(reqOptions, tools);
res.send(apiResponse);
} catch (e) {
if (e.Unauthorized) {
try {
await getRefreshToken(req, res);
const apiResponse = OTP.events.modify(reqOptions, tools);
} catch (e) {
// ...
}
} else {
return res.status(500).send(error.message);
}
} both look like a mess for me.... |
@des-des the new changes include the otp_sdk, used async/await to make the code bit more readable. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please limit use of axios + async/await to the otp_sdk folder
errorMessage: res.locals.localText.serverError, | ||
}); | ||
} else { | ||
console.log(decodedToken.access_token); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🔥
src/otp_sdk/get_refresh_token.js
Outdated
|
||
module.exports = (req, res) => { | ||
const requestToken = token => { | ||
console.log(1); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🔥
src/otp_sdk/get_refresh_token.js
Outdated
}); | ||
}; | ||
|
||
return new Promise(async (resolve, reject) => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay,
This promise wrapper is unnecessary
also,
This is the final change I will request,
There should be a clear distinction between something that is a controller, and something that is not a controller.
This file should be more like a plain function, it should take a bunch or arguments, and return a promise.
Ie, remove req and res from this function, and deal with these outcomes inside a controller. This may require a bit of work
@des-des made the changes and got rid of async/await in the controller |
rel #94
right now im showing
Try again!
on the client side for when i do get a refresh token but not sure how to restart or make the request to the api without the user clicking submit again any ideas ?