Skip to main content

Several sites on one deployment

Serve sign-in for several Nuxt applications on different domains from one Convex deployment.

One Convex deployment can serve several Nuxt applications on different domains, for example one site per customer. Each site keeps its own sign-in, cookies, and email links. Convex functions still decide which site a user may change.

How it works

  1. Each Nuxt application sets its own auth.origin, such as https://bakery.example.
  2. The auth proxy of that application signs its origin with BCN_AUTH_PROXY_IP_SECRET and sends it with each auth request to Convex.
  3. Convex checks the signature and that the origin is SITE_URL or one of siteOrigins. It then runs Better Auth with that origin as the base URL and the only trusted origin.

Better Auth therefore sets cookies for the site that the visitor uses, and email links and redirects point back to that site. A request with a wrong signature or an origin that is not in the list fails. The browser cannot choose the origin.

Configure Convex

List the other origins in createBetterConvexAuth. SITE_URL stays the main origin.

convex/auth.ts
export const auth = createBetterConvexAuth<DataModel>(components.betterAuth, {
  siteOrigins: ['https://bakery.example', 'https://florist.example'],
})

To keep the list in the database, pass a function. Convex calls it for each auth request with the HTTP action context:

convex/auth.ts
export const auth = createBetterConvexAuth<DataModel>(components.betterAuth, {
  siteOrigins: async (ctx) => await ctx.runQuery(internal.sites.origins, {}),
})

Each entry must be an exact origin: scheme, host, and port only. A * is rejected; there are no wildcard origins. Keep the query small, because every auth request runs it. If the function throws or returns something else than a list of origins, the auth request fails.

Configure each Nuxt application

Give every Nuxt application:

  • its own auth.origin (or SITE_URL),
  • the same NUXT_PUBLIC_CONVEX_URL and NUXT_PUBLIC_CONVEX_SITE_URL,
  • the same BCN_AUTH_PROXY_IP_SECRET as the Convex deployment.

Without BCN_AUTH_PROXY_IP_SECRET, the proxy sends no origin, and Convex uses SITE_URL. This is only for loopback development.

Limits

  • The OAuth provider and MCP (oauth, oauthProvider) always use SITE_URL as the issuer. createBetterConvexAuth rejects siteOrigins together with them.
  • Social sign-in providers need the callback URL of every site, for example https://bakery.example/api/auth/callback/google.
  • Users and sessions are shared by the deployment. A session cookie is set per domain, so a user signs in on each site separately. Check in every Convex function that the user is a member of the site that the function changes. See the example below.
  • A session is not bound to one site. A session cookie copied from one site works on another. Cookies are separated by host name, not by port, so treat every service on a site's host name as trusted.
  • Do not set BETTER_AUTH_TRUSTED_ORIGINS. Better Auth would add those origins next to the verified one, so the auth setup rejects it.

Check site membership in every function

A signed-in user may belong to one site and not another. Keep the site in your data (sites and siteMembers tables) and pass the site as an argument. Never read the site from a request header or the browser origin.

convex/lib/sites.ts
import { ConvexError } from 'convex/values'

import type { Id } from '../_generated/dataModel'
import type { MutationCtx, QueryCtx } from '../_generated/server'
import { auth } from '../auth'

export async function requireSiteMember(ctx: QueryCtx | MutationCtx, siteId: Id<'sites'>) {
  const user = await auth.requireUser(ctx)
  const member = await ctx.db
    .query('siteMembers')
    .withIndex('by_site_and_user', (q) => q.eq('siteId', siteId).eq('userId', user.id))
    .unique()
  if (!member) throw new ConvexError({ code: 'FORBIDDEN', message: 'Not a member of this site' })
  return { user, member }
}
convex/pages.ts
import { ConvexError, v } from 'convex/values'

import { mutation } from './_generated/server'
import { requireSiteMember } from './lib/sites'

export const publish = mutation({
  args: { siteId: v.id('sites'), pageId: v.id('pages') },
  handler: async (ctx, args) => {
    await requireSiteMember(ctx, args.siteId)
    const page = await ctx.db.get(args.pageId)
    if (!page || page.siteId !== args.siteId) throw new ConvexError('Page not found')
    await ctx.db.patch(page._id, { published: true })
  },
})

The second check matters as much as the first: a member of site A must not change a page of site B by passing a page ID from B with their own siteId.