Skip to content

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.

In the browser:

  1. Install the plugin: app.use(authPlugin(createClient({ baseURL }))).
  2. Read the session: const { session } = useSession(), then session.value.data. In a template, read session.data.
  3. Show the failure of a provider sign-in: useAuthError() reads ?auth_error= once.

On the server of Nuxt:

  1. Mount core: server/routes/auth/[...path].ts holds export default authEventHandler(getAuth);. On Workers it passes the waitUntil of the Nitro preset to handle itself.
  2. Read the session: server/middleware/session.ts holds export default authSessionHandler({ auth: getAuth });.
  3. Read the session in a handler with getSession(event). A page reads useRequestEvent()?.context.session.
  4. Protect paths: authSessionHandler({ auth: getAuth, protect: (p) => p.startsWith("/account"), signInPath: "/sign-in" }).
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.

@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.
  • The package takes vue 3.5.43 or a later 3 release as a peer dependency. The Nuxt entry takes h3 1.15.11 or a later 1 release as an optional peer.
  • The subscription of useSession ends 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 it baseURL: useRequestURL().origin.
  • Else a page that calls useSession fails in the server render.
  • protect gets the path as h3 matched it: the raw request path, decoded once, with %2F and %25 kept encoded.
  • Dot segments stay, and slashes are not merged. /account/%2e%2e reaches 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 %3F and %23 into the path, so the text of h3 for /account%3F is /account?.
  • The page router of Nuxt reads another path. It cuts the text at the first ?, but not at #.
  • For example, /secret/x%23y renders /secret/[id] with the id x#y.
  • The page router also treats \ as /, resolves . and .., and matches with or without a trailing /.
  • So /account/, /account/., and /account%3F all render the page of /account.
  • The middleware runs protect on 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 } } in nuxt.config.ts, as the example does.
  • Prefer a prefix rule: the router also renders /account/ for /account.
  • The handler of core gives handle the 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).
  • protect guards 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 with useSession.
  • 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.