The Solid package
For: a developer of a Solid app, or of a Solid Start app.
What it does: this page lists the exports of the two entries, and the paths that the protect rule gets. The export checks hold this page to packages/solid/src/index.ts and packages/solid/src/start.ts.
Before you start
Section titled “Before you start”- Read The client package for the session store under the primitives.
In the browser:
- Put
AuthProviderwith one client at the root of the app. - Read the session:
const { session } = useSession(), thensession().data. - Show the failure of a provider sign-in:
useAuthError()reads?auth_error=once.
On the server of Solid Start:
- Mount core:
src/routes/auth/[...path].tsholdsconst h = authRouteHandlers(getAuth). - Export one handler per method by name, such as
export const GET = h.GET;. - Read the session:
src/middleware.tsholdsexport default createMiddleware({ onRequest: [authOnRequest({ auth: getAuth })] });. - Read the session in an API route with
getSession(event). - Read the session in server code of a page with
getSession(getRequestEvent()), not withuseSession. - Protect paths:
authOnRequest({ auth: getAuth, protect: (p) => p.startsWith("/account"), signInPath: "/sign-in" }). - On Workers, keep
authRouteHandlers(getAuth). The handlers pass thewaitUntilof the request of srvx tohandlethemselves.
Exports
Section titled “Exports”The browser entry
Section titled “The browser entry”| Export | What it does |
|---|---|
AuthProvider |
Gives a client to every primitive below it. |
useSession |
Answers a UseSession: session, an accessor of { data, error, isPending, isRefetching }, and refetch. |
useClient |
Answers the client of the nearest provider. It throws when there is none. |
useAuthError |
Answers the code of ?auth_error= in the URL, read once, or null. |
AuthErrorCode |
The type of the code that useAuthError answers: the answer of client.readRedirectError. |
UseSession |
The type that useSession answers. |
The Solid Start entry
Section titled “The Solid Start entry”@authtempo/solid/start is for the server of Solid Start 2.
| Export | What it does |
|---|---|
authRouteHandlers |
Answers the GET, POST, and OPTIONS of src/routes/auth/[...path].ts. Export each method by name. |
authOnRequest |
Answers the onRequest that sets locals.session. It sends a person with no session on a protected path to the sign-in page. It writes the renewed cookie to the response headers of the event. |
getSession |
Answers the session that the middleware set, or null. |
AuthSource |
The auth instance, or a function that makes it at the first request. |
StartEventLike |
The fields of the event that the entry reads. |
StartApiEventLike |
The fields of the API event that the handlers read: request, and nativeEvent.req for the hook of the runtime. |
StartRouteOptions |
The options of authRouteHandlers: waitUntil, the hook that keeps work alive after the answer. Default: the waitUntil of the request of srvx. |
SolidStartAuthOptions |
The options of authOnRequest: auth, protect, signInPath, and basePath. |
In the browser
Section titled “In the browser”- The package takes
solid-js1.9.17 or a later 1 release as a peer dependency. The Solid Start entry takes@solidjs/start2.0.6 or a later 2 release as an optional peer. - The subscription of
useSessionends with the owner of the component. - A server render shows the pending state. It never subscribes to the client store, and starts no read.
- A test in a DOM needs the browser build of
solid-js. Set thebrowsercondition and inlinesolid-jsin the vitest config.
The paths that protect gets
Section titled “The paths that protect gets”protectgets the path as the routers of Solid Start 2.0.6 match it. That is the path ofrequest.urlafter the URL parse, with one/at the end removed.- The parse resolves
..,%2e, a backslash, and a tab. No escape is decoded. - So
/%61ccountreaches the rule as/%61ccount, and no route/accountmatches it. - Write a non-ASCII path encoded, with escapes in upper case:
p === "/caf%C3%A9". - Solid Router lower-cases a page path and its pattern, hex included. So
/caf%c3%a9and/CAF%C3%A9run the page/café. - An API route matches with case (radix3 1.1.2).
/MYACCOUNTruns no route/myAccount, and answers 404. - A page matches without case: Solid Router 1.0.0 compares each static part in lower case. So
/ACCOUNTruns the page/account. - Both routers remove one
/at the end, so/account/runs the route/account. radix3 runs a child route/account/:idfor/account//, with an empty id. - So the entry asks
protectwith the path as matched, and with each/at the end removed. The path/stays/. - It also asks with both forms in lower case, and with the lower-case forms with each escape in upper case.
- A path needs a session when the rule holds for any form. A rule
p === "/account"protects/ACCOUNT,/account/,/account//, and/ACCOUNT/. - Warning: a rule with an upper-case letter does not protect a page in another case.
p.startsWith("/myAccount")holds for neither/MYACCOUNTnor/myaccount, and Solid Router runs the page/myAccountfor both.- For a page, write the rule in lower case:
protect: (p) => p.toLowerCase().startsWith("/myaccount"). - The rule gets the full path. Under a
server.baseURL, write the rule with that base.
On the server of Solid Start
Section titled “On the server of Solid Start”- A person with no session on a protected path gets a 307 to the sign-in page, and no route runs.
- A server function runs at
/_server, so aprotectrule for a page does not cover the server functions that the page calls. - Read
getSession(getRequestEvent())in each server function that needs a session. - The middleware writes the renewed cookie to the response headers of the event.
- The handlers of core give
handlethewaitUntilofevent.nativeEvent.req, the request of srvx. srvx sets it on Workers. The SMS of a phone code and the flush of the events run under it after the answer (ruling 29 of plan 12i). - Solid Start 2.0.6 (h3 2.0.2) puts those headers on the answer of a page, and on the answer of an API route below status 400.
- The origin of the redirect comes from the Host header, else from the origin of
auth.baseURL. A request line in absolute form never sets it. - The middleware reads no session for a route of core. Core reads and renews the session itself.
- The middleware skips the session read only when the raw path and the matched path are both under the base path. Else it judges the request as a page.