fix(#947): only call refresh when auth token is expired#1087
fix(#947): only call refresh when auth token is expired#1087anjarupnik wants to merge 1 commit intosidebase:mainfrom
Conversation
commit: |
| = useAuthState() | ||
|
|
||
| if (refreshToken.value && token.value) { | ||
| if (refreshToken.value && !token.value) { |
There was a problem hiding this comment.
While I fully agree with the proposed implementation, I think that we still should conditionally add the access token to headers as it was before to keep compatibility with servers which expect it:
| if (refreshToken.value && !token.value) { | |
| if (refreshToken.value) { |
// include header in case of auth is required to avoid 403 rejection
const headers = token.value
? new Headers({
[provider.token.headerName]: token.value
} as HeadersInit)
: undefinedOtherwise it can be considered a breaking change.
Note for the context: local provider would likely be deprecated in version 2 due to sometimes ambiguous implementations like the current one, where library doesn't know the full usecase, such as what is required by the underlying back-end for the refresh call. Instead, users will be able to customize the behaviour and exact requirements of their backend when #1062 lands (currently pending team review).
|
Hi @anjarupnik and thanks for taking the time to fix a long-standing issue. I've left review above regarding compatibility which I believe needs to be preserved to land this. |
🔗 Linked issue
Fixes #947
❓ Type of change
📚 Description
The server-side refresh plugin (refresh-token.server.ts) triggers a token refresh with the condition refreshToken.value && token.value. This means it only refreshes when both the refresh token and the auth token are present — but this is the opposite of what's needed.
When the auth token expires, it's no longer available in the server request. However, the refresh token is still present and should be used to obtain a new auth token. With the current logic, the refresh is never triggered in this scenario.
This PR flips the condition to refreshToken.value && !token.value, so the refresh is called when a refresh token exists but the auth token is missing/expired. It also removes the Authorization header from the refresh request, since there is no valid token to send.
📝 Checklist