Repository navigation
refactor: forward set-cookie from the signOut response - #107
Merged
Merged
Conversation
logout copied Set-Cookie values from a Headers object onto the Hono context through a hand-rolled helper. Hono's c.header replaces rather than appends, which is how two of the three clearing cookies were dropped before #103. Ask for the signOut response itself with asResponse: true and append its set-cookie values to the redirect Response. No code in this repo then decides how cookies are forwarded, so a future handler cannot lose headers the same way. forwardCookies and its module are gone.
asResponse makes better-auth return APIErrors as error responses instead of throwing them (dispatch.mjs:193,249,258). logout never read the status, so a failed signOut redirected to /?message=Bye! while the session still stood. Return the signOut response and log the status when it is not ok.
Collaborator
|
This is great, @mroderick! :) |
mroderick
marked this pull request as ready for review
November 11, 2026 21:07
till
approved these changes
Nov 12, 2026
This was referenced Nov 12, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Logout now asks better-auth for the
signOutresponse and appends itsSet-Cookievalues to the redirect, so no code in this repo decides how cookies are forwarded.forwardCookiesandsrc/app/utils/cookies.jsare gone.Why this over #103
#103 fixed the symptom:
c.header("Set-Cookie", ...)replaces instead of appending, so two of the three clearing cookies vanished. It kept the hand-rolled copy loop and passed{ append: true }.This removes the class of bug instead of fixing an instance of it:
Set-Cookie. The values go from the better-authResponseonto the redirectResponseuntouched, andResponse.headers.appendis the transport-level operation rather than a framework context helper with its own semantics.signOutdecides which cookies to clear; adding or renaming one cannot desynchronise a local list.asResponse: trueis better-auth's own API, so the number of cookies is whatever better-auth returns. If a future version clears four, all four arrive.Why not a better-auth helper
There is none for this direction.
better-auth/cookiesexportsapplySetCookiesandsetCookieToHeader, but both write a requestcookieheader:applySetCookiesends intarget.set("cookie", ...), and inside better-auth the only consumer ofsetCookieToHeaderis the test harness, ascookieSetter.splitSetCookieHeaderonly splits a comma-joined header. Cookie-name constants do exist (getCookies(auth.options)), but this code needs none.Equivalence
The #103 tests are unchanged and pass, and the
/logoutresponse is byte-identical between the two implementations:One test is added, for the failure path described below.
Review notes
asResponse: truealso changes error handling, which is the part worth checking. better-auth throws anAPIErrorwhen an endpoint fails, but withasResponseit returns an errorResponsewith that status instead (node_modules/better-auth/dist/api/dispatch.mjs:193,249,258). Without a status check, logout would redirect to/?message=Bye!after a failed sign-out while the session still stood. The handler returns the failure instead, and the new test covers it by injecting an auth instance whosesignOutanswers 500.Outside error responses the call always resolves to a
Response:signOutreturned 200 with all three clearing cookies for a valid session, a bogus session token, and no session at all.Scope: based on
test/cookie-attributes, so it carries #103's tests and shows only the mechanism change. Take this one or #103, not both. If this lands, #104'sforwardCookiestest loses its subject and should be dropped with it.