Help me with my middleware oauth retry request, using refresh token
See original GitHub issueI am developing a middleware that puts my autorizathion token in the request, and identifies if it has experienced, has expired, I make a new request with the refresh token, and then I need to redo the initial request, but I only have thenext()
inside the response, How do I change the old token for the new one? and the next does not accept parameters.
response(next) {
return new Promise((resolve, reject) => {
next()
.then(resp => resolve(resp))
.catch((resp) => {
const accessToken = resp.request().headers().authorization;
const refreshToken = retrieveRefreshToken();
const status = resp.responseStatus;
if (accessToken && refreshToken && status === 401) {
refreshAuth(refreshToken, resp)
.then((newRequest) => {
next(); // in here i need insert new token. ps: request.response().enhance() not working
})
.catch(() => {
reject(resp);
});
} else {
reject(resp);
}
});
});
},
Issue Analytics
- State:
- Created 5 years ago
- Comments:6 (3 by maintainers)
Top Results From Across the Web
Using Refresh Tokens in ASP.NET Core Authentication
Refresh tokens are credentials that can be used to acquire new access tokens. When access tokens expire, we can use refresh tokens to...
Read more >What Are Refresh Tokens and How to Use Them Securely
This post will explore the concept of refresh tokens as defined by OAuth 2.0. We will learn how they compare to other token...
Read more >Retry logic with different access token in Angular http client
One user can have different refresh tokens for different API, and I don't know how to manage access token when access token is...
Read more >Refresh Tokens - OAuth 2.0 Simplified
To use the refresh token, make a POST request to the service's token endpoint with grant_type=refresh_token , and include the refresh token as ......
Read more >Using Axios interceptors for refreshing your API token.
I'm using Redis to store the token and refresh token received from the API as you can see in the examples. Obvious code...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Version
2.20.0
is out, give it a try. I’ll consider this solved for now but feel free to create a new issue or to comment on this one.Thanks for the reporting!
Hey, commit 6d85a110c75d5fc43fc8323cd40d6e788840940d introduces a new feature called “renew”. Now, from the response phase of a middleware you can re-run the middleware stack.
example:
In this example, I’m running the middleware stack after receiving a token expired (401). I think you can adapt this to your needs.
If you can, please try out the code in master. I’ll probably release a new version tomorrow. What do you think about the solution, would it solve your problem? Thanks.
NOTE: I’ve played with different options, including accepting a new request object in the
next
function but this could cause some issues when you have a stack with several middlewares configured. This approach preserves the lifecycle and makes sure all middlewares are re-executed.