refactor: Centralize cookie management with singleton pattern#10442
Conversation
|
Important Review skippedAuto incremental reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the You can disable this status message by setting the WalkthroughThis PR introduces a centralized cookie management system and refactors the authentication context login flow to eliminate race conditions. A new Changes
Sequence DiagramsequenceDiagram
actor User
participant AuthContext
participant mutateLoggedUser
participant mutateGetGlobalVariables
participant CookieManager
User->>AuthContext: login()
AuthContext->>CookieManager: setIsAuthenticated(false)
AuthContext->>mutateLoggedUser: trigger mutation
AuthContext->>mutateGetGlobalVariables: trigger mutation
par User Data Flow
mutateLoggedUser->>mutateLoggedUser: fetch user data
mutateLoggedUser->>AuthContext: onSuccess callback<br/>(set user, admin flag)
AuthContext->>AuthContext: userLoaded = true<br/>checkAndSetAuthenticated()
and Variables Flow
mutateGetGlobalVariables->>mutateGetGlobalVariables: fetch global variables
mutateGetGlobalVariables->>AuthContext: onSettled callback<br/>(set variablesLoaded)
AuthContext->>AuthContext: variablesLoaded = true<br/>checkAndSetAuthenticated()
end
AuthContext->>AuthContext: Both loaded?<br/>setIsAuthenticated(true)
AuthContext->>User: ✓ Authentication complete
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes
Possibly related PRs
Suggested labels
Suggested reviewers
Pre-merge checks and finishing touches❌ Failed checks (1 error, 2 warnings)
✅ Passed checks (4 passed)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Codecov Report❌ Patch coverage is ❌ Your project status has failed because the head coverage (39.37%) is below the target coverage (60.00%). You can increase the head coverage or adjust the target coverage. Additional details and impacted files@@ Coverage Diff @@
## main #10442 +/- ##
==========================================
+ Coverage 31.32% 31.41% +0.08%
==========================================
Files 1324 1325 +1
Lines 59920 59985 +65
Branches 8966 8979 +13
==========================================
+ Hits 18769 18843 +74
+ Misses 40254 40237 -17
- Partials 897 905 +8
Flags with carried forward coverage won't be shown. Click here to find out more.
🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
Actionable comments posted: 1
📜 Review details
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (9)
src/frontend/src/contexts/__tests__/authContext-login-fix.test.tsx(1 hunks)src/frontend/src/contexts/authContext.tsx(3 hunks)src/frontend/src/controllers/API/api.tsx(0 hunks)src/frontend/src/controllers/API/queries/auth/use-post-logout.ts(2 hunks)src/frontend/src/controllers/API/queries/auth/use-post-refresh-access.ts(2 hunks)src/frontend/src/customization/utils/custom-get-access-token.ts(1 hunks)src/frontend/src/stores/authStore.ts(1 hunks)src/frontend/src/utils/__tests__/cookie-manager.test.ts(1 hunks)src/frontend/src/utils/cookie-manager.ts(1 hunks)
💤 Files with no reviewable changes (1)
- src/frontend/src/controllers/API/api.tsx
🧰 Additional context used
📓 Path-based instructions (7)
src/frontend/src/**/*.{ts,tsx,js,jsx}
📄 CodeRabbit inference engine (.cursor/rules/frontend_development.mdc)
src/frontend/src/**/*.{ts,tsx,js,jsx}: All frontend TypeScript and JavaScript code should be located under src/frontend/src/ and organized into components, pages, icons, stores, types, utils, hooks, services, and assets directories as per the specified directory layout.
Use React 18 with TypeScript for all UI components in the frontend.
Format all TypeScript and JavaScript code using the make format_frontend command.
Lint all TypeScript and JavaScript code using the make lint command.
Files:
src/frontend/src/utils/cookie-manager.tssrc/frontend/src/contexts/__tests__/authContext-login-fix.test.tsxsrc/frontend/src/customization/utils/custom-get-access-token.tssrc/frontend/src/stores/authStore.tssrc/frontend/src/contexts/authContext.tsxsrc/frontend/src/utils/__tests__/cookie-manager.test.tssrc/frontend/src/controllers/API/queries/auth/use-post-refresh-access.tssrc/frontend/src/controllers/API/queries/auth/use-post-logout.ts
src/frontend/src/utils/**/*.{ts,tsx,js,jsx}
📄 CodeRabbit inference engine (.cursor/rules/frontend_development.mdc)
All utility functions should be placed in the utils directory.
Files:
src/frontend/src/utils/cookie-manager.tssrc/frontend/src/utils/__tests__/cookie-manager.test.ts
src/frontend/src/**/__tests__/**/*.{ts,tsx,js,jsx}
📄 CodeRabbit inference engine (.cursor/rules/frontend_development.mdc)
All frontend code should be tested using appropriate component and integration tests.
Files:
src/frontend/src/contexts/__tests__/authContext-login-fix.test.tsxsrc/frontend/src/utils/__tests__/cookie-manager.test.ts
src/frontend/**/*.@(test|spec).{ts,tsx,js,jsx}
📄 CodeRabbit inference engine (.cursor/rules/testing.mdc)
src/frontend/**/*.@(test|spec).{ts,tsx,js,jsx}: Frontend test files should be located in 'src/frontend/' and use '.test.{ts,tsx,js,jsx}' or '.spec.{ts,tsx,js,jsx}' extensions.
Test both sync and async code paths in frontend test files.
Mock external dependencies appropriately in frontend test files to isolate unit tests from external services.
Test error handling and edge cases in frontend test files.
Validate input/output behavior and test component initialization and configuration in frontend test files.
Each frontend test should have a clear description or comment explaining its purpose, especially for complex setups or mocks.
Files:
src/frontend/src/contexts/__tests__/authContext-login-fix.test.tsxsrc/frontend/src/utils/__tests__/cookie-manager.test.ts
**/{test_*.py,*.test.ts,*.test.tsx}
📄 CodeRabbit inference engine (coderabbit-custom-pre-merge-checks-unique-id-file-non-traceable-F7F2B60C-1728-4C9A-8889-4F2235E186CA.txt)
**/{test_*.py,*.test.ts,*.test.tsx}: Check if tests have too many mock objects that obscure what’s actually being tested
Warn when mocks are used instead of testing real behavior and interactions
Suggest using real objects or simpler test doubles when mocks become excessive
Ensure mocks are used only for external dependencies, not core business logic
Recommend integration tests when unit tests become overly mocked
Check that test files follow the project’s naming conventions (backend: test_*.py; frontend: *.test.ts/tsx)
Verify that tests actually exercise the new or changed functionality, not placeholder assertions
Test files should have descriptive test function names explaining what is being tested
Organize tests logically with proper setup and teardown
Include edge cases and error conditions for comprehensive coverage
Verify tests cover both positive (success) and negative (failure) scenarios
Ensure tests are not mere smoke tests; they should validate behavior thoroughly
Ensure tests follow the project’s testing frameworks (pytest for backend, Playwright for frontend)
Files:
src/frontend/src/contexts/__tests__/authContext-login-fix.test.tsxsrc/frontend/src/utils/__tests__/cookie-manager.test.ts
**/*.test.{ts,tsx}
📄 CodeRabbit inference engine (coderabbit-custom-pre-merge-checks-unique-id-file-non-traceable-F7F2B60C-1728-4C9A-8889-4F2235E186CA.txt)
Frontend tests should be named *.test.ts or *.test.tsx and use Playwright
Files:
src/frontend/src/contexts/__tests__/authContext-login-fix.test.tsxsrc/frontend/src/utils/__tests__/cookie-manager.test.ts
src/frontend/src/stores/**/*.{ts,tsx,js,jsx}
📄 CodeRabbit inference engine (.cursor/rules/frontend_development.mdc)
Use Zustand for state management in frontend stores.
Files:
src/frontend/src/stores/authStore.ts
🧠 Learnings (15)
📚 Learning: 2025-07-21T14:16:14.125Z
Learnt from: CR
PR: langflow-ai/langflow#0
File: .cursor/rules/testing.mdc:0-0
Timestamp: 2025-07-21T14:16:14.125Z
Learning: Applies to src/frontend/**/*.@(test|spec).{ts,tsx,js,jsx} : Mock external dependencies appropriately in frontend test files to isolate unit tests from external services.
Applied to files:
src/frontend/src/contexts/__tests__/authContext-login-fix.test.tsxsrc/frontend/src/utils/__tests__/cookie-manager.test.ts
📚 Learning: 2025-07-21T14:16:14.125Z
Learnt from: CR
PR: langflow-ai/langflow#0
File: .cursor/rules/testing.mdc:0-0
Timestamp: 2025-07-21T14:16:14.125Z
Learning: Applies to src/frontend/**/*.@(test|spec).{ts,tsx,js,jsx} : Validate input/output behavior and test component initialization and configuration in frontend test files.
Applied to files:
src/frontend/src/contexts/__tests__/authContext-login-fix.test.tsxsrc/frontend/src/utils/__tests__/cookie-manager.test.ts
📚 Learning: 2025-07-21T14:16:14.125Z
Learnt from: CR
PR: langflow-ai/langflow#0
File: .cursor/rules/testing.mdc:0-0
Timestamp: 2025-07-21T14:16:14.125Z
Learning: Applies to src/frontend/**/*.@(test|spec).{ts,tsx,js,jsx} : Test both sync and async code paths in frontend test files.
Applied to files:
src/frontend/src/contexts/__tests__/authContext-login-fix.test.tsxsrc/frontend/src/utils/__tests__/cookie-manager.test.ts
📚 Learning: 2025-07-21T14:16:14.125Z
Learnt from: CR
PR: langflow-ai/langflow#0
File: .cursor/rules/testing.mdc:0-0
Timestamp: 2025-07-21T14:16:14.125Z
Learning: Applies to src/frontend/**/*.@(test|spec).{ts,tsx,js,jsx} : Each frontend test should have a clear description or comment explaining its purpose, especially for complex setups or mocks.
Applied to files:
src/frontend/src/contexts/__tests__/authContext-login-fix.test.tsxsrc/frontend/src/utils/__tests__/cookie-manager.test.ts
📚 Learning: 2025-07-21T14:16:14.125Z
Learnt from: CR
PR: langflow-ai/langflow#0
File: .cursor/rules/testing.mdc:0-0
Timestamp: 2025-07-21T14:16:14.125Z
Learning: Applies to src/frontend/**/*.@(test|spec).{ts,tsx,js,jsx} : Test error handling and edge cases in frontend test files.
Applied to files:
src/frontend/src/contexts/__tests__/authContext-login-fix.test.tsxsrc/frontend/src/utils/__tests__/cookie-manager.test.ts
📚 Learning: 2025-10-23T19:53:43.132Z
Learnt from: CR
PR: langflow-ai/langflow#0
File: coderabbit-custom-pre-merge-checks-unique-id-file-non-traceable-F7F2B60C-1728-4C9A-8889-4F2235E186CA.txt:0-0
Timestamp: 2025-10-23T19:53:43.132Z
Learning: Applies to **/{test_*.py,*.test.ts,*.test.tsx} : Ensure mocks are used only for external dependencies, not core business logic
Applied to files:
src/frontend/src/contexts/__tests__/authContext-login-fix.test.tsx
📚 Learning: 2025-07-18T18:27:12.609Z
Learnt from: CR
PR: langflow-ai/langflow#0
File: .cursor/rules/frontend_development.mdc:0-0
Timestamp: 2025-07-18T18:27:12.609Z
Learning: Applies to src/frontend/src/**/__tests__/**/*.{ts,tsx,js,jsx} : All frontend code should be tested using appropriate component and integration tests.
Applied to files:
src/frontend/src/contexts/__tests__/authContext-login-fix.test.tsxsrc/frontend/src/utils/__tests__/cookie-manager.test.ts
📚 Learning: 2025-06-23T12:46:42.048Z
Learnt from: CR
PR: langflow-ai/langflow#0
File: .cursor/rules/frontend_development.mdc:0-0
Timestamp: 2025-06-23T12:46:42.048Z
Learning: Frontend tests should be written using testing-library/react, with both component and integration tests verifying rendering, user interaction, and data loading.
Applied to files:
src/frontend/src/contexts/__tests__/authContext-login-fix.test.tsx
📚 Learning: 2025-10-23T19:53:43.132Z
Learnt from: CR
PR: langflow-ai/langflow#0
File: coderabbit-custom-pre-merge-checks-unique-id-file-non-traceable-F7F2B60C-1728-4C9A-8889-4F2235E186CA.txt:0-0
Timestamp: 2025-10-23T19:53:43.132Z
Learning: Applies to **/{test_*.py,*.test.ts,*.test.tsx} : Recommend integration tests when unit tests become overly mocked
Applied to files:
src/frontend/src/contexts/__tests__/authContext-login-fix.test.tsx
📚 Learning: 2025-10-23T19:53:43.132Z
Learnt from: CR
PR: langflow-ai/langflow#0
File: coderabbit-custom-pre-merge-checks-unique-id-file-non-traceable-F7F2B60C-1728-4C9A-8889-4F2235E186CA.txt:0-0
Timestamp: 2025-10-23T19:53:43.132Z
Learning: Applies to **/{test_*.py,*.test.ts,*.test.tsx} : Verify that tests actually exercise the new or changed functionality, not placeholder assertions
Applied to files:
src/frontend/src/contexts/__tests__/authContext-login-fix.test.tsxsrc/frontend/src/utils/__tests__/cookie-manager.test.ts
📚 Learning: 2025-06-23T12:46:42.048Z
Learnt from: CR
PR: langflow-ai/langflow#0
File: .cursor/rules/frontend_development.mdc:0-0
Timestamp: 2025-06-23T12:46:42.048Z
Learning: Use Zustand for state management in React components within the frontend; stores should expose both state and setter functions, and be imported via hooks (e.g., useMyStore).
Applied to files:
src/frontend/src/stores/authStore.tssrc/frontend/src/contexts/authContext.tsxsrc/frontend/src/controllers/API/queries/auth/use-post-refresh-access.ts
📚 Learning: 2025-07-18T18:27:12.609Z
Learnt from: CR
PR: langflow-ai/langflow#0
File: .cursor/rules/frontend_development.mdc:0-0
Timestamp: 2025-07-18T18:27:12.609Z
Learning: Applies to src/frontend/src/stores/**/*.{ts,tsx,js,jsx} : Use Zustand for state management in frontend stores.
Applied to files:
src/frontend/src/stores/authStore.ts
📚 Learning: 2025-10-23T19:53:43.132Z
Learnt from: CR
PR: langflow-ai/langflow#0
File: coderabbit-custom-pre-merge-checks-unique-id-file-non-traceable-F7F2B60C-1728-4C9A-8889-4F2235E186CA.txt:0-0
Timestamp: 2025-10-23T19:53:43.132Z
Learning: Applies to **/{test_*.py,*.test.ts,*.test.tsx} : Include edge cases and error conditions for comprehensive coverage
Applied to files:
src/frontend/src/utils/__tests__/cookie-manager.test.ts
📚 Learning: 2025-10-23T19:53:43.132Z
Learnt from: CR
PR: langflow-ai/langflow#0
File: coderabbit-custom-pre-merge-checks-unique-id-file-non-traceable-F7F2B60C-1728-4C9A-8889-4F2235E186CA.txt:0-0
Timestamp: 2025-10-23T19:53:43.132Z
Learning: Applies to **/{test_*.py,*.test.ts,*.test.tsx} : Ensure tests are not mere smoke tests; they should validate behavior thoroughly
Applied to files:
src/frontend/src/utils/__tests__/cookie-manager.test.ts
📚 Learning: 2025-10-23T19:53:43.132Z
Learnt from: CR
PR: langflow-ai/langflow#0
File: coderabbit-custom-pre-merge-checks-unique-id-file-non-traceable-F7F2B60C-1728-4C9A-8889-4F2235E186CA.txt:0-0
Timestamp: 2025-10-23T19:53:43.132Z
Learning: Applies to **/{test_*.py,*.test.ts,*.test.tsx} : Organize tests logically with proper setup and teardown
Applied to files:
src/frontend/src/utils/__tests__/cookie-manager.test.ts
🧬 Code graph analysis (7)
src/frontend/src/contexts/__tests__/authContext-login-fix.test.tsx (1)
src/frontend/src/contexts/authContext.tsx (2)
AuthProvider(32-157)AuthContext(30-30)
src/frontend/src/customization/utils/custom-get-access-token.ts (2)
src/frontend/src/utils/cookie-manager.ts (1)
cookieManager(50-50)src/frontend/src/constants/constants.ts (1)
LANGFLOW_ACCESS_TOKEN(862-862)
src/frontend/src/stores/authStore.ts (1)
src/frontend/src/utils/cookie-manager.ts (1)
getCookiesInstance(51-51)
src/frontend/src/contexts/authContext.tsx (2)
src/frontend/src/utils/cookie-manager.ts (1)
getCookiesInstance(51-51)src/frontend/src/controllers/API/index.ts (1)
checkHasStore(202-211)
src/frontend/src/utils/__tests__/cookie-manager.test.ts (1)
src/frontend/src/utils/cookie-manager.ts (2)
getCookiesInstance(51-51)cookieManager(50-50)
src/frontend/src/controllers/API/queries/auth/use-post-refresh-access.ts (1)
src/frontend/src/utils/cookie-manager.ts (1)
getCookiesInstance(51-51)
src/frontend/src/controllers/API/queries/auth/use-post-logout.ts (1)
src/frontend/src/utils/cookie-manager.ts (1)
getCookiesInstance(51-51)
🪛 Gitleaks (8.28.0)
src/frontend/src/utils/__tests__/cookie-manager.test.ts
[high] 90-90: Detected a Generic API Key, potentially exposing access to various services and sensitive operations.
(generic-api-key)
[high] 163-163: Detected a Generic API Key, potentially exposing access to various services and sensitive operations.
(generic-api-key)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (41)
- GitHub Check: Run Frontend Tests / Playwright Tests - Shard 40/40
- GitHub Check: Run Frontend Tests / Playwright Tests - Shard 39/40
- GitHub Check: Run Frontend Tests / Playwright Tests - Shard 24/40
- GitHub Check: Run Frontend Tests / Playwright Tests - Shard 30/40
- GitHub Check: Run Frontend Tests / Playwright Tests - Shard 38/40
- GitHub Check: Run Frontend Tests / Playwright Tests - Shard 33/40
- GitHub Check: Run Frontend Tests / Playwright Tests - Shard 25/40
- GitHub Check: Run Frontend Tests / Playwright Tests - Shard 27/40
- GitHub Check: Run Frontend Tests / Playwright Tests - Shard 31/40
- GitHub Check: Run Frontend Tests / Playwright Tests - Shard 37/40
- GitHub Check: Run Frontend Tests / Playwright Tests - Shard 36/40
- GitHub Check: Run Frontend Tests / Playwright Tests - Shard 35/40
- GitHub Check: Run Frontend Tests / Playwright Tests - Shard 34/40
- GitHub Check: Run Frontend Tests / Playwright Tests - Shard 29/40
- GitHub Check: Run Frontend Tests / Playwright Tests - Shard 20/40
- GitHub Check: Run Frontend Tests / Playwright Tests - Shard 18/40
- GitHub Check: Run Frontend Tests / Playwright Tests - Shard 32/40
- GitHub Check: Run Frontend Tests / Playwright Tests - Shard 28/40
- GitHub Check: Run Frontend Tests / Playwright Tests - Shard 21/40
- GitHub Check: Run Frontend Tests / Playwright Tests - Shard 22/40
- GitHub Check: Run Frontend Tests / Playwright Tests - Shard 26/40
- GitHub Check: Run Frontend Tests / Playwright Tests - Shard 19/40
- GitHub Check: Run Frontend Tests / Playwright Tests - Shard 23/40
- GitHub Check: Run Frontend Tests / Playwright Tests - Shard 14/40
- GitHub Check: Run Frontend Tests / Playwright Tests - Shard 17/40
- GitHub Check: Run Frontend Tests / Playwright Tests - Shard 15/40
- GitHub Check: Run Frontend Tests / Playwright Tests - Shard 16/40
- GitHub Check: Run Frontend Tests / Playwright Tests - Shard 13/40
- GitHub Check: Run Frontend Tests / Playwright Tests - Shard 10/40
- GitHub Check: Run Frontend Tests / Playwright Tests - Shard 12/40
- GitHub Check: Run Frontend Tests / Playwright Tests - Shard 9/40
- GitHub Check: Run Frontend Tests / Playwright Tests - Shard 11/40
- GitHub Check: Run Frontend Tests / Playwright Tests - Shard 6/40
- GitHub Check: Run Frontend Tests / Playwright Tests - Shard 8/40
- GitHub Check: Run Frontend Tests / Playwright Tests - Shard 7/40
- GitHub Check: Run Frontend Tests / Playwright Tests - Shard 3/40
- GitHub Check: Run Frontend Tests / Playwright Tests - Shard 1/40
- GitHub Check: Run Frontend Tests / Playwright Tests - Shard 5/40
- GitHub Check: Run Frontend Tests / Playwright Tests - Shard 4/40
- GitHub Check: Run Frontend Tests / Playwright Tests - Shard 2/40
- GitHub Check: Test Starter Templates
…i/langflow into cz/fix-cookies-race-condition
…i/langflow into cz/fix-cookies-race-condition
…ow-ai#10442) * add cookies factory to prevent race condition * add tests * [autofix.ci] apply automated fixes * add retry timeout on auth * add login page control to remove old sessions * fix auth context tests * add max retries to auth cookies settings * add folder auth option * fix nth children progress track test * fix user progress test * fix user progress track test validation screen * fix login redirect on switch between users --------- Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
This pull request refactors cookie management throughout the frontend codebase by introducing a centralized singleton
CookieManagerutility. This change replaces all direct instantiations of theCookiesclass with calls to the new manager, ensuring consistent and synchronized cookie handling across the application. Additionally, comprehensive unit tests are added for the new utility to verify its correctness and address previous race condition issues.Cookie Management Refactor:
CookieManagerclass insrc/frontend/src/utils/cookie-manager.ts, providing unifiedget,set, andremovemethods, and exportedcookieManagerandgetCookiesInstancefor use throughout the app.Cookieswith usage ofcookieManagerorgetCookiesInstancein files such asauthContext.tsx, API controllers, and stores, ensuring all components use the same cookie instance. [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12]Testing and Reliability:
src/frontend/src/utils/__tests__/cookie-manager.test.tsto verify the singleton pattern, correct cookie operations, integration scenarios, edge cases, and to ensure the fix for the previous race condition (issue fix: Add output item processing for tool results #10348).These changes improve reliability and maintainability of authentication and session management by eliminating desynchronization issues and potential race conditions previously caused by multiple cookie instances.
Summary by CodeRabbit
Bug Fixes
Tests