Skip to content

feat: add setRequestToken and fetchRequestToken methods#900

Open
susnux wants to merge 3 commits intomainfrom
feat/settoken
Open

feat: add setRequestToken and fetchRequestToken methods#900
susnux wants to merge 3 commits intomainfrom
feat/settoken

Conversation

@susnux
Copy link
Contributor

@susnux susnux commented Mar 3, 2026

Basically move them from server to this library, implementation only slightly changed for the non-dom case to keep service worker support. Otherwise same implementation and even tests are copied.

This allows us to have 1 place with the set token logic to reduce problems where the tokens get out of sync.

@susnux susnux requested review from ShGKme and artonge March 3, 2026 11:02
@susnux susnux added enhancement New feature or request 3. to review labels Mar 3, 2026
@susnux susnux force-pushed the feat/settoken branch 2 times, most recently from cfc893c to b7d9bc6 Compare March 3, 2026 11:05
@codecov
Copy link

codecov bot commented Mar 3, 2026

Codecov Report

❌ Patch coverage is 89.28571% with 3 lines in your changes missing coverage. Please review.
✅ Project coverage is 65.75%. Comparing base (2ea86ef) to head (21b1c82).

Files with missing lines Patch % Lines
lib/requestToken.ts 89.28% 1 Missing and 2 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #900      +/-   ##
==========================================
+ Coverage   60.71%   65.75%   +5.03%     
==========================================
  Files           4        4              
  Lines          56       73      +17     
  Branches       17       23       +6     
==========================================
+ Hits           34       48      +14     
- Misses         20       21       +1     
- Partials        2        4       +2     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@susnux susnux requested a review from artonge March 3, 2026 12:38
Comment on lines +18 to +24
export function getRequestToken(): string | null {
if (globalThis.document) {
return document.head.dataset.requesttoken ?? null
}
// for service workers or other contexts without DOM, we keep the token in memory
return globalThis._nc_auth_requestToken ?? null
}
Copy link
Contributor

Choose a reason for hiding this comment

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

In the previous implementation it only used DOM API once to initiate the local state. Now every getRequestToken call gets it via DOM API.

What about checking _nc_auth_requestToken first and only as a fallback get it from the document to init the local state?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Then we still have a problem if updated outside of this library (aka old server versions).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

(I mean we can do so and hope to update server in time - but still wonder how much overhead the DOM API call would be here)

Copy link
Contributor

Choose a reason for hiding this comment

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

What is the problem?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

  • If we change the token here in an app A
  • Then another app B causes a token change in server
  • Now this app A tries to get the token and gets the outdated because server only updated the DOM
  • App A needs 2 useless requests to recover

Copy link
Contributor

Choose a reason for hiding this comment

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

Doesn't setToken on server emit the event even on a very old server?
nextcloud/server#17404

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah yes seems to work.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

let me adjust this

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I have adjusted the implementation to use the cached version if possible.
Also prevented an event loop

Copy link
Contributor Author

Choose a reason for hiding this comment

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

(see fixup commit)

susnux added 3 commits March 3, 2026 15:14
Signed-off-by: Ferdinand Thiessen <opensource@fthiessen.de>
Signed-off-by: Ferdinand Thiessen <opensource@fthiessen.de>
@susnux susnux requested a review from ShGKme March 3, 2026 17:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

3. to review enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants