Skip to content

fix(android): sync Cronet and token-refresh requests with CookieManager#73

Merged
riteshshukla04 merged 6 commits into
margelo:mainfrom
oferRounds:pr/android-cookiemanager-cold-start
May 4, 2026
Merged

fix(android): sync Cronet and token-refresh requests with CookieManager#73
riteshshukla04 merged 6 commits into
margelo:mainfrom
oferRounds:pr/android-cookiemanager-cold-start

Conversation

@oferRounds
Copy link
Copy Markdown
Contributor

@oferRounds oferRounds commented Apr 11, 2026

Summary

Aligns Android Cronet (NitroFetchClient) and the HttpURLConnection cold-start token-refresh path (AutoPrefetcher) with CookieManager: attach Cookie when the request has no Cookie header, and persist Set-Cookie from responses (including redirects).

Helps SAML/session flows where the session cookie lives in the WebView cookie jar.

Split from #71

This is PR 1 of 2 from the discussion on #71 (atomic PRs per review).

PR 2 (upstream): #74 — cold-start token-refresh outcome → JS (draft until this PR lands; rebase after merge).

Outcome-only diff vs this branch (fork stack): oferRounds#1

How to verify

Exercise cold-start prefetch + token refresh on Android with a session that relies on cookies set via WebView; confirm requests include the expected cookie and refreshed cookies are stored.

- NitroFetchClient: attach Cookie from CookieManager when request has no Cookie header;
 persist Set-Cookie from responses (including redirects).
- AutoPrefetcher: same for HttpURLConnection token refresh; persist Set-Cookie from refresh response.

Helps SAML/session flows where the session cookie lives in the WebView cookie jar.

Made-with: Cursor
@riteshshukla04
Copy link
Copy Markdown
Collaborator

Hey @oferRounds , Thanks for the PR .
The code looks correct. Just few minor changes needed

@oferRounds
Copy link
Copy Markdown
Contributor Author

Sure, let me know what :)

@riteshshukla04
Copy link
Copy Markdown
Collaborator

Sure, let me know what :)

Can you please look at my review comments 😅

}
}
}
cookieManager.flush()
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This will run everytime even though we dont have cookies . Maybe we can add check here to run only if actually set-cookie exist

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Also maybe we flush once on onSucceeded . Idk if thats good . Ideally we should not flush on every redirect

for (header in setCookieHeaders) {
cookieManager.setCookie(responseUrl, header.value)
}
cookieManager.flush()
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Same as above

conn.doInput = true
if (body != null) conn.doOutput = true

var hasCookieHeader = false
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Lets create this in a function like NitroFetchClient.kt . Lets avoid 2 code doing the exact thing but in different ways

@oferRounds
Copy link
Copy Markdown
Contributor Author

good catches! Let me fix them

- NitroCookieSync: shared attach + Set-Cookie helpers; flush only when cookies applied
- Cronet: apply Set-Cookie on redirects without flush; flush once on success when
  redirects or final response stored cookies
- HttpURLConnection token refresh: reuse attach helper; Set-Cookie + flush only if needed

Made-with: Cursor
@oferRounds
Copy link
Copy Markdown
Contributor Author

Addressed the review feedback in a follow-up commit:

  • Set-Cookie / flush: Only flush when at least one Set-Cookie was applied; HttpURLConnection token refresh uses the same rule.
  • Cronet redirects: Apply Set-Cookie on each redirect without flush; one CookieManager.flush() on onSucceeded when any hop (redirect or final) stored cookies—so redirect-only cookies still persist if the final response has no Set-Cookie.
  • Deduped attach logic: New NitroCookieSync.kt with attachCookieFromManagerIfMissing + header checks used by both NitroFetchClient and AutoPrefetcher.

Let me know if you want anything tweaked.

HttpURLConnection follows redirects by default; conn.url returns the
final URL, which is the correct domain to associate Set-Cookie with.

Made-with: Cursor
@riteshshukla04
Copy link
Copy Markdown
Collaborator

Thanks. Looks much better now. Will see to merge tomorrow :)

@riteshshukla04
Copy link
Copy Markdown
Collaborator

So just one minor thing after discussing this internally . Can we make this opt-in and configurable. Because requests that were previously anonymous may now become authenticated or behave differently . So lets avoid this breaking change

…eSync()

Cookie sync is now disabled by default — requests that were previously
anonymous stay that way unless the consumer explicitly calls
NitroCookieSync.enableCookieSync() from Application.onCreate.

All public methods (attach, store, flush) short-circuit with a no-op
when disabled.

Addresses: margelo#73 (comment)

Made-with: Cursor
@oferRounds
Copy link
Copy Markdown
Contributor Author

Done! Pushed a follow-up commit (25f5968):

Cookie sync is now opt-inNitroCookieSync is disabled by default and every public method (attachCookieFromManagerIfMissing, storeSetCookie*, flushCookieManager) is a no-op until the consumer calls:

// e.g. in MainApplication.onCreate, before AutoPrefetcher.prefetchOnStart
NitroCookieSync.enableCookieSync()

This way existing consumers see zero behavior change, and apps that rely on WebView cookie bridging can explicitly opt in.

@vercel
Copy link
Copy Markdown

vercel Bot commented May 4, 2026

@riteshshukla04 is attempting to deploy a commit to the Margelo Team on Vercel.

A member of the Team first needs to authorize it.

@riteshshukla04 riteshshukla04 merged commit 44eeb03 into margelo:main May 4, 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.

2 participants