Skip to content

fix(auth): sign out only after definitive session expiry - #414

Merged
jeromevdl merged 1 commit into
aws-samples:mainfrom
Alae-Tamouh:contrib/definitive-session-expiry
Sep 1, 2026
Merged

fix(auth): sign out only after definitive session expiry#414
jeromevdl merged 1 commit into
aws-samples:mainfrom
Alae-Tamouh:contrib/definitive-session-expiry

Conversation

@Alae-Tamouh

Copy link
Copy Markdown
Contributor

Issue #, if available: None

Description of changes:

Problem

When Cognito can no longer refresh a session, API calls fail with 401 but AuthContext retains the previous user. The UI remains authenticated and continues issuing failing requests.
Logging out on every API 401 is unsafe because provider endpoints can also return GitHub, GitLab, Bitbucket, or Jira 401 responses while the Cognito session remains valid.

image image

Reproducer

  1. Sign in and open any authenticated page.
  2. In another browser tab or through the Cognito administration API, revoke the user's refresh token or invalidate the session.
  3. Return to the application after the access token expires and trigger an API request, such as refreshing the spaces list.
  4. Observe that the request fails with 401, but the application still considers the previous user authenticated and remains on the protected page.
  5. Trigger additional actions and observe repeated 401 responses instead of a return to the login page.

Solution

Classify session resolution as valid, definitively expired, or transiently unavailable. On an API 401, force a Cognito refresh before notifying AuthContext. A small epoch-aware bridge coalesces expiry notifications and prevents late requests from an old session from signing out a newly authenticated local or SSO session.

Impact

Expired users return cleanly to login. Network failures and provider-specific 401 responses no longer cause false logout, and stale requests cannot expire a replacement session.

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

Comment thread frontend/src/services/api.ts
Comment thread frontend/src/services/auth.ts
Comment thread frontend/src/contexts/AuthContext.test.tsx

@jeromevdl jeromevdl 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.

Great finding @Alae-Tamouh. I never faced this so thank you for your PR.
A few comments before approving.

@JWThewes

JWThewes commented Sep 1, 2026

Copy link
Copy Markdown
Contributor

@Alae-Tamouh are you going to work on Jeromes comments? Otherwise I can work on this one. Super great finding an I'm eager to get this one in for our next release.

@Alae-Tamouh
Alae-Tamouh force-pushed the contrib/definitive-session-expiry branch from b1b03c5 to ab7e50a Compare September 1, 2026 14:13
@Alae-Tamouh

Copy link
Copy Markdown
Contributor Author

@JWThewes Thanks for the reminder, I addressed Jerome's comments

@Alae-Tamouh
Alae-Tamouh force-pushed the contrib/definitive-session-expiry branch 2 times, most recently from 93cc632 to ab7e50a Compare September 1, 2026 14:50
@jeromevdl
jeromevdl merged commit 71e5624 into aws-samples:main Sep 1, 2026
8 checks passed
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