Skip to content

feat: Enhance session management with logout - SFS-3105 - #3268

Merged
eduardoformiga merged 2 commits into
devfrom
fix/login-again-agter-logout
Apr 23, 2026
Merged

eduardoformiga merged 2 commits into
devfrom
fix/login-again-agter-logout

Conversation

@eduardoformiga

@eduardoformiga eduardoformiga commented Apr 10, 2026 •

Copy link
Copy Markdown
Member

What's the purpose of this pull request?

  • Enhance session management with logout handling and validation tracking

How it works?

  • Added SESSION_READY_KEY to manage session readiness state after logout.
  • Updated checkAndReloadIfReturnedFromLogout to set session readiness on reload.
  • Modified createValidationStore to include hasValidatedStore for tracking validation completion.
  • Adjusted useSession and useSessionReady hooks to utilize hasValidated for improved session state management, addressing React 18 batching issues.

How to test it?

using b2b store url.

Summary by CodeRabbit

  • Bug Fixes

    • Improved session state management to ensure proper tracking after user logout and return.
    • Enhanced session readiness detection for more reliable authentication handling.
  • Refactor

    • Optimized internal session validation logic for better performance and state consistency.

…tracking

- Added SESSION_READY_KEY to manage session readiness state after logout.
- Updated checkAndReloadIfReturnedFromLogout to set session readiness on reload.
- Modified createValidationStore to include hasValidatedStore for tracking validation completion.
- Adjusted useSession and useSessionReady hooks to utilize hasValidated for improved session state management, addressing React 18 batching issues.
@eduardoformiga
eduardoformiga requested a review from a team as a code owner April 10, 2026 02:32
@eduardoformiga
eduardoformiga requested review from gabpaladino and lariciamota and removed request for a team April 10, 2026 02:32
@coderabbitai

coderabbitai Bot commented Apr 10, 2026 •

Copy link
Copy Markdown
Contributor

Walkthrough

Replaces a locally defined reload key with shared storage keys, adds a persistent "hasValidated" store, updates session validation flow to short-circuit on a logout-return flag, and defers marking session-ready until validation has completed.

Changes

Cohort / File(s) Summary
Session core & readiness
packages/core/src/sdk/session/index.ts
Reworked session validation flow: useSessionReady now accepts hasValidated; added isReloadAfterLogoutPending() that reads RELOAD_AFTER_LOGOUT_KEY; validateSession returns early when reload-after-logout is pending; SESSION_READY_KEY moved to storageKeys.
Validation store
packages/core/src/sdk/useStore.ts
createValidationStore now returns a 3-tuple: adds hasValidatedStore which is set true after the first validation completes.
Logout-return hook
packages/core/src/components/account/MyAccountDrawer/OrganizationDrawer/useReloadAfterLogoutReturn.ts
Replaced local RELOAD_AFTER_LOGOUT_KEY with imported constant from sdk/session/storageKeys; when handling logout-return, sets SESSION_READY_KEY in sessionStorage before scheduling a delayed reload.
Storage keys
packages/core/src/sdk/session/storageKeys.ts
Added exported RELOAD_AFTER_LOGOUT_KEY and SESSION_READY_KEY constants for centralized session-related storage keys.

Sequence Diagram(s)

sequenceDiagram
  participant Client
  participant SessionHook as Session Hook
  participant ValidationStore as Validation Store
  participant sessionStorage as sessionStorage

  Client->>SessionHook: mount/useSession()
  SessionHook->>ValidationStore: createValidationStore(validateSession)
  SessionHook->>sessionStorage: read RELOAD_AFTER_LOGOUT_KEY?
  alt reload-after-logout present
    SessionHook->>sessionStorage: remove RELOAD_AFTER_LOGOUT_KEY
    SessionHook->>sessionStorage: set SESSION_READY_KEY = "true"
    SessionHook->>Client: schedule forceRefreshWithoutCache (delayed)
  else normal flow
    SessionHook->>ValidationStore: onValidate() -> validateSession()
    ValidationStore-->>ValidationStore: set hasValidated = true (finally)
    ValidationStore-->>SessionHook: isValidating / hasValidated updates
    SessionHook->>SessionHook: useSessionReady(!isValidating && hasValidated)
    SessionHook-->>Client: session ready state true/false
  end
Loading

Estimated Code Review Effort

🎯 4 (Complex) | ⏱️ ~45 minutes

Possibly Related PRs

Suggested Reviewers

  • lariciamota

Poem

🔁 Keys moved, validations learn to stay,
One true flag remembers the first day,
Logout whispers, a reload waits,
Session readies once the check completes,
Small flags keeping state in play. ✨

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly summarizes the main change: enhancing session management with logout handling, directly reflected in the code changes across session hooks and storage keys.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/login-again-agter-logout

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@codesandbox-ci

codesandbox-ci Bot commented Apr 10, 2026 •

Copy link
Copy Markdown

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@packages/core/src/sdk/session/index.ts`:
- Line 19: The SDK imports a UI component constant RELOAD_AFTER_LOGOUT_KEY from
MyAccountDrawer, creating a UI→SDK coupling; create a neutral module (e.g., add
packages/core/src/sdk/session/storageKeys.ts) that exports
RELOAD_AFTER_LOGOUT_KEY (and any other keys like SESSION_READY_KEY), then update
packages/core/src/sdk/session/index.ts to import RELOAD_AFTER_LOGOUT_KEY from
that new storageKeys module instead of the component path so SDK business logic
no longer depends on UI implementation details.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: e4c1e50f-e839-4cd7-aaf2-60fef31b9007

📥 Commits

Reviewing files that changed from the base of the PR and between 9495157 and 2882f7d.

📒 Files selected for processing (3)
  • packages/core/src/components/account/MyAccountDrawer/OrganizationDrawer/useReloadAfterLogoutReturn.ts
  • packages/core/src/sdk/session/index.ts
  • packages/core/src/sdk/useStore.ts

Comment thread packages/core/src/sdk/session/index.ts Resolved
@eduardoformiga eduardoformiga changed the title feat: Enhance session management with logout handling and validation … feat: Enhance session management with logout - SFS-3105 Apr 10, 2026

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
packages/core/src/components/account/MyAccountDrawer/OrganizationDrawer/useReloadAfterLogoutReturn.ts (1)

45-56: ⚠️ Potential issue | 🟡 Minor

Consider timing of SESSION_READY_KEY pre-set before page reload. ⏱️

Setting SESSION_READY_KEY = 'true' on line 50 happens ~1s before the hard navigation. During that window, when the return page initializes its session store, useSessionReady will:

  1. Initialize isSessionReady state to true (from the pre-set flag)
  2. Run its validation effect, which guards with if (isValidating || !hasValidated) return

Components reading the store state during this brief window could render as "ready" before validation completes. However, this is mitigated by the hard reload occurring within ~1s, and the validation flow on the reloaded page will always run to correct the state regardless of the pre-seeded flag.

If you want to eliminate this timing window entirely, move the setItem call immediately before window.location.replace inside forceRefreshWithoutCache to shrink the observable gap. Otherwise, the current pattern is acceptable if you're confident validation completes within ~1s on typical page hydration.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In
`@packages/core/src/components/account/MyAccountDrawer/OrganizationDrawer/useReloadAfterLogoutReturn.ts`
around lines 45 - 56, The SESSION_READY_KEY is being set in
checkAndReloadIfReturnedFromLogout before the delayed hard navigation, creating
a brief window where components may see the session as ready; move the session
pre-set into forceRefreshWithoutCache immediately before performing the
navigation (i.e., remove sessionStorage.setItem(SESSION_READY_KEY, 'true') from
checkAndReloadIfReturnedFromLogout and instead set
sessionStorage.setItem(SESSION_READY_KEY, 'true') inside
forceRefreshWithoutCache just prior to calling window.location.replace) so the
observable gap between setting SESSION_READY_KEY and the reload is eliminated;
keep use of RELOAD_DELAY_MS and setTimeout(call to forceRefreshWithoutCache)
intact.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Outside diff comments:
In
`@packages/core/src/components/account/MyAccountDrawer/OrganizationDrawer/useReloadAfterLogoutReturn.ts`:
- Around line 45-56: The SESSION_READY_KEY is being set in
checkAndReloadIfReturnedFromLogout before the delayed hard navigation, creating
a brief window where components may see the session as ready; move the session
pre-set into forceRefreshWithoutCache immediately before performing the
navigation (i.e., remove sessionStorage.setItem(SESSION_READY_KEY, 'true') from
checkAndReloadIfReturnedFromLogout and instead set
sessionStorage.setItem(SESSION_READY_KEY, 'true') inside
forceRefreshWithoutCache just prior to calling window.location.replace) so the
observable gap between setting SESSION_READY_KEY and the reload is eliminated;
keep use of RELOAD_DELAY_MS and setTimeout(call to forceRefreshWithoutCache)
intact.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: ae649692-0ce3-4a01-88af-9904bc668909

📥 Commits

Reviewing files that changed from the base of the PR and between 2882f7d and 1281263.

📒 Files selected for processing (3)
  • packages/core/src/components/account/MyAccountDrawer/OrganizationDrawer/useReloadAfterLogoutReturn.ts
  • packages/core/src/sdk/session/index.ts
  • packages/core/src/sdk/session/storageKeys.ts
✅ Files skipped from review due to trivial changes (1)
  • packages/core/src/sdk/session/storageKeys.ts
🚧 Files skipped from review as they are similar to previous changes (1)
  • packages/core/src/sdk/session/index.ts

@lemagnetic lemagnetic left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants