Issue with Updating Access Token in Custom Middleware #4796
Unanswered
danieljcksn
asked this question in
Help
Replies: 1 comment
-
Great question @danieljcksn. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hello AdonisJS community,
I'm facing an issue in AdonisJS (version 6) while working on a custom authentication middleware. The problem occurs when refreshing an expired access token. Even after a successful token refresh and updating
ctx.request.headers().authorization
, the middleware appears to re-execute and continues using the old, expired token. I understand thatctx.request.headers()
seems to be read-only, which adds complexity to directly updating the headers.The Problem
When an access token expires, the middleware refreshes it and attempts to update
ctx.request.headers().authorization
, which is always set asBearer ${accessToken}
. However, what does not persist between the initial request (before the token refresh) and subsequent requests is the cookies set during the refresh process. This causes the middleware to re-execute and continue using the old, expired token, leading to failed authentication.Middleware Code
Refresh Token Controller
My Question
Given that
ctx.request.headers()
is read-only and cannot be effectively updated to reflect the new token, how can I ensure that the updatedctx.request.headers().authorization
is recognized and used throughout the current request lifecycle? Is there a recommended approach to handle this scenario so that subsequent middleware or route handlers acknowledge the new token?I appreciate any guidance or suggestions from the community. Thank you for your help!
Environment Details
Beta Was this translation helpful? Give feedback.
All reactions