The Vue package
For: a developer of a Vue app, or of a Nuxt 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/vue/src/index.ts and packages/vue/src/nuxt.ts.
Before you start
Section titled “Before you start”- Read The client package for the session store under the composables.
In the browser:
- Install the plugin:
app.use(authPlugin(createClient({ baseURL }))). - Read the session:
const { session } = useSession(), thensession.value.data. In a template, readsession.data. - Show the failure of a provider sign-in:
useAuthError()reads?auth_error=once.
On the server of Nuxt:
- Mount core:
server/routes/auth/[...path].tsholdsexport default authEventHandler(getAuth);. On Workers it passes thewaitUntilof the Nitro preset tohandleitself. - Read the session:
server/middleware/session.tsholdsexport default authSessionHandler({ auth: getAuth });. - Read the session in a handler with
getSession(event). A page readsuseRequestEvent()?.context.session. - Protect paths:
authSessionHandler({ auth: getAuth, protect: (p) => p.startsWith("/account"), signInPath: "/sign-in" }).
Exports
Section titled “Exports”The browser entry
Section titled “The browser entry”| Export | What it does |
|---|---|
authPlugin |
Answers the plugin that gives a client to every composable: app.use(authPlugin(client)). |
useSession |
Answers a UseSession: session, a ref of { data, error, isPending, isRefetching }, and refetch. |
useClient |
Answers the client of the plugin. It throws when the plugin is not installed. |
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 Nuxt entry
Section titled “The Nuxt entry”@authtempo/vue/nuxt is for the server of Nuxt, which runs on h3 1.
| Export | What it does |
|---|---|
authEventHandler |
Answers the handler of server/routes/auth/[...path].ts. |
authSessionHandler |
Answers the server middleware that sets event.context.session. It sends a person with no session on a protected path to the sign-in page, and adds the renewed cookie. |
getSession |
Answers the session that the middleware set, or null. |
AuthSource |
The auth instance, or a function that makes it at the first request, so nuxt build reads no secret. |
NuxtRouteOptions |
The options of authEventHandler: waitUntil, the hook that keeps work alive after the answer. Default: event.context.waitUntil, else event.context.cloudflare.context.waitUntil. |
NuxtAuthOptions |
The options of authSessionHandler: auth, protect, signInPath, and basePath. |
In the browser
Section titled “In the browser”- The package takes
vue3.5.43 or a later 3 release as a peer dependency. The Nuxt entry takesh31.15.11 or a later 1 release as an optional peer. - The subscription of
useSessionends with the scope of the component. - A server render shows the pending state. It never subscribes to the client store, and starts no read.
- In Nuxt, install the plugin on both sides:
app/plugins/authtempo.ts, not.client.ts. Give itbaseURL: useRequestURL().origin. - Else a page that calls
useSessionfails in the server render.
The paths that protect gets
Section titled “The paths that protect gets”protectgets the path as h3 matched it: the raw request path, decoded once, with%2Fand%25kept encoded.- Dot segments stay, and slashes are not merged.
/account/%2e%2ereaches the rule as/account/.., which a child route/account/[id]matches. - A non-ASCII route reaches the rule decoded: write
p === "/café", not"/caf%C3%A9". - h3 decodes
%3Fand%23into the path, so the text of h3 for/account%3Fis/account?. - The page router of Nuxt reads another path. It cuts the text at the first
?, but not at#. - For example,
/secret/x%23yrenders/secret/[id]with the idx#y. - The page router also treats
\as/, resolves.and.., and matches with or without a trailing/. - So
/account/,/account/., and/account%3Fall render the page of/account. - The middleware runs
protecton four forms, each once, and on each form in lower case. The first form is the text of h3, and the second is that text cut at the first?. - The third form is the page path: cut at
?, with\as/,.and..resolved, and no trailing/. - The page path is still decoded, so
/café/gives/café. The fourth form, an extra one, is the text cut at the first#. - A path needs a session when the rule holds for any form. So an exact rule
p === "/account"guards every path that renders that page. - An extra form only adds a redirect.
- Vue Router matches without case by default. The middleware runs each form in lower case, so write the rule in lower case.
- A rule with capital letters, such as
p.startsWith("/myAccount"), misses/myaccount, which runs the same page. - You can also set
router: { options: { sensitive: true } }innuxt.config.ts, as the example does. - Prefer a prefix rule: the router also renders
/account/for/account.
On the server of Nuxt
Section titled “On the server of Nuxt”- The handler of core gives
handlethe hook of the platform that Nitro puts in the event context. The Cloudflare presets of Nitro 2 set it. The SMS of a phone code and the flush of the events run under it after the answer. On Node, the work runs detached (ruling 29 of plan 12i). protectguards a page load from the server only. A<NuxtLink>navigation renders the page in the browser, and never reaches the server middleware.- So read private data only in a server route that calls
getSession(event). Else read the session in the browser withuseSession. - Or link with plain
<a>tags, so that each page load reaches the server. - The session middleware reads the URL and the headers only, never the body.
- The session 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 h3 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.
- On Node, the origin of the redirect comes from the protocol of h3 and the Host header. It never comes from a request line in absolute form.
- A Host that does not parse as one host falls back to the origin of
auth.baseURL, so no request answers 500. - A web handler, such as a Worker preset, keeps the URL of the request, and the origin comes from that URL.