The Astro package
For: a developer of an Astro app with on-demand pages (output: "server").
What it does: this page lists the exports of the package, the steps of the mount, and the path that the protect rule gets. The export check holds this page to packages/astro/src/index.ts.
Before you start
Section titled “Before you start”- Read The API reference for the auth object that the package takes.
- Mount core:
src/pages/auth/[...path].tsholdsexport const ALL = authRoute(getAuth);andexport const prerender = false;. - Read the session:
src/middleware.tsholdsexport const onRequest = authMiddleware({ auth: getAuth });. - Read the session in a page:
Astro.locals.session. - Protect paths:
authMiddleware({ auth: getAuth, protect: (p) => p.startsWith("/account"), signInPath: "/sign-in" }). - On Workers with
@astrojs/cloudflare, keepauthRoute(getAuth). The endpoint passeslocals.cfContext.waitUntiltohandleitself.
Exports
Section titled “Exports”| Export | What it does |
|---|---|
authRoute |
Answers the endpoint of the routes of core. |
authMiddleware |
Answers the onRequest that sets locals.session, sends a person with no session on a protected path to the sign-in page, and adds the renewed cookie. |
AuthSource |
The auth instance, or a function that makes it at the first request, so astro build reads no secret. |
AstroRouteOptions |
The options of authRoute: waitUntil, the hook that keeps work alive after the answer. Default: locals.cfContext.waitUntil of the Cloudflare adapter. |
AstroAuthOptions |
The options of authMiddleware: auth, protect, signInPath, and basePath. |
The path that protect gets
Section titled “The path that protect gets”protectgets the text that the router of Astro matched, fromcontext.originPathname.- That text is decoded until it stops changing, with duplicate slashes merged and the case kept.
- It has no trailing “/”, and no
baseof the Astro config. - A non-ASCII route reaches the rule decoded (
"/café"). Write the rule for that form. - The adapter resolves a literal or once-escaped dot segment before the match:
/account/..is/. A twice-escaped one stays in the text:/account/%252e%252e/xreaches the rule as/account/../x. A prefix rule covers it. - The
nextof the sign-in redirect comes fromcontext.url.
How it behaves
Section titled “How it behaves”- On Workers,
authRoutegiveshandlethewaitUntiloflocals.cfContext, which@astrojs/cloudflare14 sets. The SMS of a phone code and the flush of the events run under it after the answer. With no Cloudflare adapter, the work runs detached. PassauthRoute(getAuth, { waitUntil })to use another hook (ruling 29 of plan 12i). - The package takes
astro7.3.8 or a later 7 release as a peer dependency. getAuthis a function that makes the auth instance at the first request, soastro buildreads no secret.- 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.
- Core matches its routes on the raw path. A path that Astro decodes to a path of core, such as
/%61uth/x, never runs a page. - When the rule holds for the decoded path and there is no session, the answer is the sign-in redirect. Else core answers 404.
- After the sliding refresh, the page’s answer carries the renewed session cookie.
- Set
security.allowedDomainsto the host of your app. Without it, Astro gives each request the originlocalhost. - Then the sign-in redirect goes to
localhost, and Astro refuses a form post from your app. - The rule is
readGateof@authtempo/core/bindings, the same in every server package.