fix(ai-chat): refresh JWT token on SSE reconnect to prevent login red…#20176
Open
b3nito404 wants to merge 1 commit intotwentyhq:mainfrom
Open
fix(ai-chat): refresh JWT token on SSE reconnect to prevent login red…#20176b3nito404 wants to merge 1 commit intotwentyhq:mainfrom
b3nito404 wants to merge 1 commit intotwentyhq:mainfrom
Conversation
…irect during streaming
Contributor
Welcome!
Hello there, congrats on your first PR! We're excited to have you contributing to this project. |
Contributor
There was a problem hiding this comment.
1 issue found across 1 file
Prompt for AI agents (unresolved issues)
Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.
<file name="packages/twenty-front/src/modules/sse-db-event/hooks/useHandleSseClientConnectionRetry.ts">
<violation number="1" location="packages/twenty-front/src/modules/sse-db-event/hooks/useHandleSseClientConnectionRetry.ts:65">
P1: Unconditionally writing renewed tokens from a shared in-flight promise can overwrite a newer auth/session state if the user logs out or switches accounts during renewal.</violation>
</file>
Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.
| ) | ||
| .then((tokens) => { | ||
| if (isDefined(tokens)) { | ||
| store.set(tokenPairState.atom, tokens); |
Contributor
There was a problem hiding this comment.
P1: Unconditionally writing renewed tokens from a shared in-flight promise can overwrite a newer auth/session state if the user logs out or switches accounts during renewal.
Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At packages/twenty-front/src/modules/sse-db-event/hooks/useHandleSseClientConnectionRetry.ts, line 65:
<comment>Unconditionally writing renewed tokens from a shared in-flight promise can overwrite a newer auth/session state if the user logs out or switches accounts during renewal.</comment>
<file context>
@@ -20,21 +31,53 @@ export const useHandleSseClientConnectionRetry = () => {
+ )
+ .then((tokens) => {
+ if (isDefined(tokens)) {
+ store.set(tokenPairState.atom, tokens);
+ }
+ return true;
</file context>
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #18928
Problem
When a JWT access token expires while the AI chat is streaming a response, the SSE connection drops and
graphql-ssecalls the retry callback. The previous implementation would wait, then destroy the SSE client but never refreshed the token. On the next connection attempt the client reused the same expired token, eventually triggering anUNAUTHENTICATEDerror that redirected the user to the login screen.Solution
Add proactive token renewal inside
useHandleSseClientConnectionRetrybefore each reconnect attempt:let renewalPromisevariable to deduplicate concurrent renewal requests , the exactpattern used inApolloFactory.tsrenewTokenviaretryWithBackoffagainst the/metadataendpointheaders()callback picks it up automatically on reconnectFiles changed
packages/twenty-front/src/modules/sse-db-event/hooks/useHandleSseClientConnectionRetry.tsNotes
This addresses the two issues from the previous review:
useRefusing module-level variable insteadCombinedGraphQLErrorsimport