Skip to content

fix: fully clear DA session on sign-out and enrich unauthorized errors#10

Merged
davidnuescheler merged 1 commit into
mainfrom
fix/da-session-cleanup
Jul 7, 2026
Merged

fix: fully clear DA session on sign-out and enrich unauthorized errors#10
davidnuescheler merged 1 commit into
mainfrom
fix/da-session-cleanup

Conversation

@davidnuescheler

Copy link
Copy Markdown
Contributor

Problem

After connecting, users hit Error invoking remote method 'da:list': Error: Unauthorized: invalid or expired token — even after #9, and especially users who had signed in before.

#9 cleared the .da-token.json file, but the app keeps credentials in four more places that all survived sign-out and 401 recovery:

  • per-site preview tokens in .site-tokens.json (+ its in-memory cache)
  • the content.da.live bearer-token cache
  • IMS session cookies in the persistent persist:aem-preview-login partition (deliberately persistent so refreshes complete silently) and the persist:aem-preview webview partition

So a previously signed-in user's IMS session kept silently re-minting credentials for the old account.

Changes

  • Complete session invalidation (src/main/da-session.js): invalidateDaSession removes the token file, empties .site-tokens.json, resets token/preview caches, and clears IMS/DA cookies + web storage across the default, preview, and preview-login sessions. Runs on explicit sign-out and automatically on any 401.
  • Richer unauthorized errors: every 401 in ContentApiClient now throws an HttpRequestError carrying method, URL, status, x-error header, and body detail — still prefixed with Unauthorized: invalid or expired token so existing detection keeps matching. The main process appends the site (org/repo + backend) and IMS JWT diagnostics (client_id, user, issued/expires — never the token value) and logs the same line to the persisted log under the da-auth scope.
  • No more silent browser logins: withContentClient resolves the stored token instead of getValidToken, so a background request with an expired token fails fast with the diagnostic error instead of popping a browser (also removes a port-9898 race under concurrent requests). The renderer shows "AEM sign-in expired or was rejected — sign in again" with the detail in the tooltip/console.

If a user still hits a 401 after a truly fresh sign-in, the error now shows whether the token is stale (old issued timestamp ⇒ cached somewhere) or fresh-but-rejected (⇒ da.live permissions for that org).

Testing

  • 10 new tests in test/da-session.test.js (token resolution errors, JWT diagnostics, storage clearing across partitions, full invalidation)
  • enriched-401 test in test/content-api-client.test.js
  • npm run lint and npm test green (201 tests); dev app boots clean

🤖 Generated with Claude Code

Signing out (or recovering from a 401) only deleted the IMS token file.
Site tokens, in-memory caches, and IMS session cookies in the persistent
preview and preview-login partitions survived, so a previously signed-in
user's session kept re-minting credentials and da:list failed with
"Unauthorized: invalid or expired token" again after re-login.

- invalidateDaSession (da-session.js) removes the token file, empties
  .site-tokens.json, resets token/preview caches, and clears IMS/DA
  cookies and web storage across the default, preview, and preview-login
  sessions; wired into da:logout and the 401 recovery path.
- 401s now throw HttpRequestError with method, URL, x-error header, and
  body detail, still prefixed with the unauthorized message so existing
  detection matches; the main process appends site and IMS JWT
  diagnostics (client_id, user, issued/expires) and logs them.
- Background requests no longer silently trigger a browser login on an
  expired token; the renderer shows why the session ended and routes to
  the explicit Sign in button.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@davidnuescheler davidnuescheler merged commit fc77820 into main Jul 7, 2026
4 checks passed
@trieloff

trieloff commented Jul 7, 2026

Copy link
Copy Markdown
Contributor

🎉 This PR is included in version 1.6.2 🎉

The release is available on GitHub release

Your semantic-release bot 📦🚀

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

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants