---
title: "Auth setup"
description: "The scaffolded backend/auth.ts and http.ts, everything setupAuth takes and returns, how it connects email, rate limits, the activity trail, erasure and launch phases, and the auth route."
canonical_url: "https://nuxt-backend.dev/docs/build/auth/setup"
---
# Auth setup

> The scaffolded backend/auth.ts and http.ts, everything setupAuth takes and returns, how it connects email, rate limits, the activity trail, erasure and launch phases, and the auth route.

Version: nuxt-backend 0.6.x · Tab: Build · Kind: reference · Status: stable

Every page: [llms.txt](https://nuxt-backend.dev/llms.txt) · Skill for coding agents: [nuxt-backend](https://nuxt-backend.dev/.well-known/skills/nuxt-backend/SKILL.md)

Commands use npm; use your project's package manager.

- [API reference](https://nuxt-backend.dev/docs/reference/api/auth)
- [Source](https://github.com/qruto/nuxt-backend/blob/main/src/convex/client/index.ts)

`setupAuth` builds your app's auth in one call, in your Convex backend (`backend/`), not in the Vue app. It runs Better Auth inside the package's `backend` component and turns on sign-in codes, passkeys and an authenticator app after a code. Then it connects them to your email, rate limits, activity trail and erasure. The scaffold calls it for you, so most apps never change this file. Read on when you add a hook, move the auth route or pass Better Auth options of your own.

## The scaffolded files

`npx nuxt module add nuxt-backend` writes them when it installs the module (`npx nuxt-backend init` does the same later), and `nuxt-backend dev` restores any that go missing ([Installation](https://nuxt-backend.dev/docs/start/installation)). Auth needs three. A fourth, `backend/convex.config.ts`, mounts the `backend` component that holds the auth tables ([How it works](https://nuxt-backend.dev/docs/start/concepts/how-it-works#the-scaffolded-convexconfigts)).

```ts [backend/auth.config.ts]
export { default } from 'nuxt-backend/auth.config'
```

```ts [backend/auth.ts]
import { setupAuth } from 'nuxt-backend/auth'
import { components, internal } from './_generated/api'
import { query } from './_generated/server'
import { account } from './account'
import { audit } from './audit'
import { email } from './email'
import { rateLimiter } from './rateLimiter'
import { workflow } from './workflows'

export const {
  authComponent,
  createAuth,
  getAuthUser,
  authConfig,
  updateProfile,
  listSessions,
  revokeSession,
} = setupAuth(components, query, {
  email,
  rateLimiter,
  audit,
  account,
  onUserCreated: async (ctx, user) => {
    await workflow.start(ctx, internal.workflows.onSignup, {
      userId: user.id,
      email: user.email,
      name: user.name,
    })
  },
})
```

```ts [backend/http.ts]
import { registerBackendRoutes } from 'nuxt-backend/http'
import { httpRouter } from 'convex/server'
import { ai } from './ai'
import { authComponent, createAuth } from './auth'
import { billing } from './billing'
import { email } from './email'

const http = httpRouter()
registerBackendRoutes(http, { auth: { authComponent, createAuth }, billing, email, ai })

export default http
```

`registerBackendRoutes` mounts the auth routes beside the billing and email webhooks and the AI stream route. Every webhook delivery is signature-verified and refused until its secret is set ([Register webhooks](https://nuxt-backend.dev/docs/ship/security/webhooks)).

## `setupAuth(components, query, options)`

It takes your generated `components`, your `query` builder and the options below, and returns:

<table>
<thead>
  <tr>
    <th>
      Name
    </th>
    
    <th>
      What it is
    </th>
  </tr>
</thead>

<tbody>
  <tr>
    <td>
      <code>
        authComponent
      </code>
    </td>
    
    <td>
      The auth component client, for <code>
        registerBackendRoutes
      </code>
      
      .
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        createAuth(ctx)
      </code>
    </td>
    
    <td>
      The Better Auth instance for one request.
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        getAuthUser
      </code>
    </td>
    
    <td>
      A query: the signed-in account, or an <code>
        unauthenticated
      </code>
      
       refusal.
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        authConfig
      </code>
    </td>
    
    <td>
      A query: the paths the deployment serves on and links to, which <code>
        doctor
      </code>
      
       compares with <code>
        nuxt.config
      </code>
      
       (<a href="https://nuxt-backend.dev/docs/reference/cli/diagnostics#page-paths">
        <code>
          page-paths
        </code>
      </a>
      
      ).
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        updateProfile
      </code>
    </td>
    
    <td>
      A mutation: the caller's <code>
        name
      </code>
      
      , <code>
        image
      </code>
      
       and <code>
        locale
      </code>
      
      . Never the email address (<a href="https://nuxt-backend.dev/docs/build/auth/accounts/profile">
        Profile
      </a>
      
      ).
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        listSessions
      </code>
      
      , <code>
        revokeSession
      </code>
    </td>
    
    <td>
      The caller's devices and signing one out, behind <code>
        useSessions()
      </code>
      
       (<a href="https://nuxt-backend.dev/docs/build/auth/accounts/security">
        Security
      </a>
      
      ).
    </td>
  </tr>
</tbody>
</table>

`listSessions` reads the session table, so it works for a sign-in of any age, and returns ids, never session tokens. `revokeSession` signs out one of the caller's own sessions through Better Auth's route, so your session hooks still run.

### Codes, not links

Every proof of a mailbox is a one-time code: sign-in, and both halves of an email change. No link signs anyone in or moves an account, so a forwarded email or a scanner that opens every link changes nothing. Better Auth's link routes are off. The database keeps only a hash of each code, keyed by `AUTH_SECRET`, so reading the table yields no code. [Security model](https://nuxt-backend.dev/docs/ship/security#passwordless-sign-in-and-its-rate-limits) has the limits on guessing.

Each code is bound to the browser that asked for it, and every code, cookie and recovery code is tagged with its purpose: a sign-in code never confirms an email change, and a recovery code never signs in as a code ([Codes work only where they were asked for](https://nuxt-backend.dev/docs/build/auth/sign-in#codes-work-only-where-they-were-asked-for)).

## Cross-component wiring

Each option connects auth to another part of your backend. All are optional but `email`:

<table>
<thead>
  <tr>
    <th>
      Option
    </th>
    
    <th>
      What it connects
    </th>
    
    <th>
      Home
    </th>
  </tr>
</thead>

<tbody>
  <tr>
    <td>
      <code>
        paths
      </code>
    </td>
    
    <td>
      The <code>
        paths
      </code>
      
       of the generated <code>
        paths.generated.ts
      </code>
      
      : the auth route, and the pages the auth emails link to (an invitation, a recovered passkey-only account, a waitlist place). The module writes the file from <code>
        nuxt.config
      </code>
      
      , so you never write a path here.
    </td>
    
    <td>
      <a href="https://nuxt-backend.dev/docs/reference/configuration/module-options#the-paths-file">
        The paths file
      </a>
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        email
      </code>
      
       (required)
    </td>
    
    <td>
      Your <code>
        setupEmail
      </code>
      
       instance. Every auth email goes out through it: codes, the welcome email, the email-change notice and invitations.
    </td>
    
    <td>
      <a href="https://nuxt-backend.dev/docs/build/email/templates">
        Templates
      </a>
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        rateLimiter
      </code>
    </td>
    
    <td>
      Limits on sending codes per address and for the whole deployment, on checking them, on email changes, and on the privileged routes. A closed limit answers <code>
        rate_limited
      </code>
      
       with the wait.
    </td>
    
    <td>
      <a href="https://nuxt-backend.dev/docs/ship/security/rate-limits">
        Tune rate limits
      </a>
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        audit
      </code>
    </td>
    
    <td>
      Your <code>
        setupAudit
      </code>
      
       instance, so the <code>
        auth
      </code>
      
       capability's events reach its <code>
        onEvent
      </code>
      
      .
    </td>
    
    <td>
      <a href="https://nuxt-backend.dev/docs/build/auth/admin/audit-log">
        Record and review activity
      </a>
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        account
      </code>
    </td>
    
    <td>
      Your <code>
        setupAccount
      </code>
      
       instance, so a deletion erases everything of the account.
    </td>
    
    <td>
      <a href="https://nuxt-backend.dev/docs/build/auth/accounts/data">
        Export and delete an account
      </a>
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        access
      </code>
    </td>
    
    <td>
      Your <code>
        setupAccess
      </code>
      
       instance: the launch phase and the tickets that admit a new address.
    </td>
    
    <td>
      <a href="https://nuxt-backend.dev/docs/build/growth/launch">
        Launch in phases
      </a>
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        workspaces
      </code>
    </td>
    
    <td>
      How long an invitation lasts (<code>
        invitationExpiresIn
      </code>
      
      , in seconds), a veto on invitations (<code>
        canInvite
      </code>
      
      ), and the workspace hooks <code>
        onCreate
      </code>
      
      , <code>
        onMemberAdded
      </code>
      
       and <code>
        onMemberRemoved
      </code>
      
      . Only these: the organization plugin behind workspaces takes no other option from here, and any other key (a 0.1 <code>
        organizationHooks
      </code>
      
      , say) throws when <code>
        setupAuth
      </code>
      
       runs, so a guard you kept never fails open.
    </td>
    
    <td>
      <a href="https://nuxt-backend.dev/docs/build/auth/workspaces/invitations">
        Invite people to a workspace
      </a>
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        canSignIn
      </code>
    </td>
    
    <td>
      Your own gate on sign-in, sign-up, new sessions and email changes.
    </td>
    
    <td>
      <a href="https://nuxt-backend.dev/docs/build/auth/sign-in/hooks#gate-who-signs-in">
        Gate who signs in
      </a>
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        onUserCreated
      </code>
      
      , <code>
        onUserDeleted
      </code>
    </td>
    
    <td>
      Your code once an account exists, or once it is deleted.
    </td>
    
    <td>
      <a href="https://nuxt-backend.dev/docs/build/auth/sign-in/hooks">
        Sign-in hooks
      </a>
    </td>
  </tr>
</tbody>
</table>

Billing links provider customers to accounts, and keeps its feature and credit cache inside the `backend` component, so subscriptions and credits need nothing in your schema.

<note>

Until `EMAIL_API_KEY` is set, a code request is refused with a message the form shows, and Resend's test addresses still get theirs. In development (`BACKEND_ENVIRONMENT=development`), every code also prints in the `convex dev` console ([Develop with real sandboxes](https://nuxt-backend.dev/docs/develop/sandboxes)).

</note>

## Auth route and options

The auth routes answer under `/api/auth` on your app's origin. The Nuxt module proxies them to the deployment.

### Extend Better Auth options

Better Auth's own options pass through: `admin` for the admin plugin, and `authOptions` for the rest. They merge with the package's defaults. `workspaces` takes the package's own options only, which it maps onto the organization plugin: an upstream option of that plugin is reachable only by ejecting `backend/auth.ts`.

```ts [backend/auth.ts]
export const { authComponent, createAuth, getAuthUser } = setupAuth(components, query, {
  email,
  authOptions: {
    appName: 'Panels',
    session: { expiresIn: 60 * 60 * 24 * 30 },
  },
  workspaces: { invitationExpiresIn: 60 * 60 * 24 * 7 },
})
```

`databaseHooks` you pass in `authOptions` compose with the package's ([Add Better Auth hooks of your own](https://nuxt-backend.dev/docs/build/auth/sign-in/hooks#add-better-auth-hooks-of-your-own)).

<note>

The `backend` component owns the auth schema. Keep data of your own in app tables keyed by the user id, not in extra fields on `user`. Leave out Better Auth plugins that add tables or fields: the packaged schema has no room for them.

</note>

### Change the auth route

Set the module's `authRoute`. It is the one place the path lives: the module writes it to `backend/paths.generated.ts`, where the auth routes, the Convex auth config and the proxy read it ([The paths file](https://nuxt-backend.dev/docs/reference/configuration/module-options#the-paths-file)).

```ts [nuxt.config.ts]
export default defineNuxtConfig({
  modules: ['nuxt-backend'],
  backend: { authRoute: '/internal/auth' },
})
```

The next `nuxt prepare` or `dev` rewrites the paths file, and the scaffolded `auth.config.ts` and `auth.ts` pass it on:

```ts [backend/auth.config.ts]
import { defineBackendAuthConfig } from 'nuxt-backend/auth.config'
import { paths } from './paths.generated'

export default defineBackendAuthConfig({ paths })
```

Push the functions with the new file: until the deployment serves the new route, the proxy forwards to one it does not, and sign-in fails. `npx nuxt-backend doctor` says so ([`page-paths`](https://nuxt-backend.dev/docs/reference/cli/diagnostics#page-paths)).

## Related

- [Sign users in](https://nuxt-backend.dev/docs/build/auth/sign-in): what the scaffolded auth gives the app.
- [Hook into sign-in and sign-up](https://nuxt-backend.dev/docs/build/auth/sign-in/hooks): `canSignIn`, the lifecycle hooks and `APIError`.
- [Environment variables](https://nuxt-backend.dev/docs/reference/configuration/environment-variables): `AUTH_SECRET`, `SITE_URL` and the `EMAIL_*` variables.
- [Security model](https://nuxt-backend.dev/docs/ship/security): what the `auth` capability turns off, and why.


## Sitemap

See the full [sitemap](https://nuxt-backend.dev/sitemap.md) for all pages.
