@authtempo/tanstack-start
v0.2.0
Published
The TanStack Start binding of authtempo, for React Start and Solid Start of TanStack. It imports nothing of TanStack, so one package serves both, and it names no peer dependency. Its typings fixture compiles against `@tanstack/react-start` 1.169.1 and `@t
Downloads
660
Keywords
Readme
@authtempo/tanstack-start
The TanStack Start binding of authtempo, for React Start and Solid Start of
TanStack. It imports nothing of TanStack, so one package serves both, and it
names no peer dependency. Its typings fixture compiles against
@tanstack/react-start 1.169.1 and @tanstack/solid-start 1.169.1.
A test also runs the real request middleware of @tanstack/solid-start in
vitest, with happy-dom, the browser build of solid-js, and an inline
transform of solid-js and @tanstack. The example app is React.
- Mount core:
src/routes/auth.$.tsholdsexport const Route = createFileRoute("/auth/$")({ server: { handlers: authRouteHandlers(getAuth) } });. - Read the session:
src/start.tsaddscreateMiddleware({ type: "request" }).server(authRequestServer({ auth: getAuth }))torequestMiddleware, and a handler readsgetSession(context). - Protect paths:
authRequestServer({ auth: getAuth, protect: (p) => p.startsWith("/account"), signInPath: "/sign-in" }).
authRouteHandlersanswers GET, POST, PUT, PATCH, DELETE, and OPTIONS: every method under the base path goes to core. TanStack answers HEAD with the GET handler.createMiddlewarecomes from@tanstack/react-startor@tanstack/solid-start.protectgets the path that the router of TanStack matches, not the raw path of the request. The router decodes the path once: "/%61ccount" reaches the rule as "/account", and "/caf%C3%A9" as "/café". "%25" and "%5C" stay escaped. The URL parser has already resolved "." and ".." segments and read "" as "/". The router removes each "/" at the end, so "/account/" reaches the rule as "/account". "/account/.." reaches it as "/", the path that the router matches.- The router matches a path without case: it compares the lower-case form of
the route with the lower-case form of the path (router-core 1.171.37). So
"/ACCOUNT" runs the page of "/account". The binding cannot read a case
option, so it runs
protecton the path, on its lower-case form, and on the lower-case form with each escape in upper-case hex ("/q%3Fx"): a path needs a session when one of them holds. The router keeps an escape such as "%3F" in the hex case of the request. Write the rule in lower case, and it holds for every case of the path. A rule with a capital letter, such asp.startsWith("/myAccount"), misses "/myaccount" and "/MYACCOUNT", which run the page of "/myAccount" with no session. - The origin of the redirect comes from the Host header, with the protocol of
the request, not from the request line: the Node server of TanStack Start
builds the URL of an absolute-form line ("GET http://evil.example/account")
from that line. A Host that does not parse falls back to the origin of
core's
baseURL. - Behind a proxy, send the public Host to the app: the binding never reads
X-Forwarded-Host, even when srvx trusts it.
auth.baseURLserves only when the Host header is missing or does not parse as one host, or the request URL is not http or https. nextholds the page URL that TanStack gives the middleware, with the query, not the decoded path: "/%61ccount" givesnext=%2Faccount, and "/%2561ccount" keeps its escape.nextkeeps the case of the request and a "/" at the end.- The middleware reads no session for a route of core, and adds the renewed cookie to the page's answer after the sliding refresh. Every Set-Cookie line of core passes unchanged.
- A server function runs at its own path, so a
protectrule for a page does not cover the server functions that the page calls. ReadgetSession(context)in each server function that needs a session. - The server reads the session from the middleware's context. A server render
never subscribes to the session store of
@authtempo/client. - A function given to both
authRouteHandlersandauthRequestServerruns twice for a request to core, so make it return one instance per process. - The rule is
readGateof@authtempo/core/bindings, the same in every server binding.
