Repository navigation
fix(auth): make authContext a globalThis singleton - #180
Merged
Merged
Conversation
In dev, Vite's SSR module runner and void's bundled runtime can each
evaluate runtime/auth.ts, producing two separate AsyncLocalStorage
instances. runVoidAuthMiddleware writes to the bundled instance while
user-land getUser()/getSession() read the SSR instance, so loaders
always saw a null user even when the cookie and session were valid.
Store the ALS on globalThis under Symbol.for("void.authContext.v1") so
duplicate module evaluations share a single instance. Add a regression
test that uses vi.resetModules() to force re-evaluation and verifies
both copies expose the same authContext, including write-through-static
read-through-fresh propagation.
TheAlexLichter
marked this pull request as ready for review
May 5, 2026 07:18
Member
|
Is storing on |
Member
Author
|
I don't think so, because of how the module is loaded via workerd and Vite's SSR runner. A separate module would still run through both "envs" |
Member
|
Yeah, I figured. I'll take a look and merge/update this PR. |
cpojer
approved these changes
May 7, 2026
cpojer
left a comment
Member
There was a problem hiding this comment.
Alright, looks like that's our only option.
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.
This PR Fixes a dev bug where
getUser()/getSession()fromvoid/authreturnednulleven thoughrunVoidAuthMiddlewarehad populated the request with a valid user.The Root cause:
runtime/auth.tswas evaluated twice:authContext.run(...)from the middleware)import { getUser } from "void/auth"). Each evaluation created its own top-levelnew AsyncLocalStorage()The fix involves storing
authContextonglobalThiskeyed bySymbol.for("void.authContext.v1"), so duplicate module evaluations within the same JS realm reuse a single AsyncLocalStorage.