Convex-Zen Authentication Component
Convex-Zen is a production-grade authentication component designed for applications leveraging the Convex backend. Currently at version 1.13.5, it offers a robust, reusable auth solution. The package is actively maintained with frequent releases, typically multiple bug fixes and dependency updates within a month, reflecting ongoing development. Its key differentiator from Convex's 'Better Auth' integration is that Convex-Zen implements native authentication logic directly within Convex component functions, rather than importing an external runtime into app code. It provides framework-specific exports, allowing seamless integration with popular environments like Next.js, Tanstack Start, and Expo, while exposing a consistent and familiar API surface for developers.
Common errors
-
Module not found: Can't resolve 'convex-zen/next'
cause This error occurs when a framework-specific component (like a provider) is imported from a subpath that doesn't exist, is misspelled, or when the bundler fails to resolve package `exports` conditions.fixDouble-check the import path for the framework-specific component (e.g., `convex-zen/next`, `convex-zen/tanstack-start`). Ensure your project's build setup correctly handles package `exports` for subpath imports. -
TypeError: (0 , _convex_zen_next__WEBPACK_IMPORTED_MODULE_0__.ConvexProviderWithAuth) is not a function
cause Often seen in bundled output, this indicates a mismatch in how modules are exported and imported, typically when a CommonJS-style `require` or incorrect named import tries to consume an ESM named export.fixVerify that your `tsconfig.json` has `"module": "ESNext"` (or a modern equivalent) and that your bundler is configured for full ESM compatibility. Confirm that `ConvexProviderWithAuth` is indeed a named export and not a default export. -
Convex client not initialized. Make sure you have wrapped your app in a <ConvexProvider>
cause While a core Convex error, it frequently surfaces when `ConvexZenProvider` (which internally provides the Convex client) is not correctly placed at the root of your application, or when the `ConvexReactClient` instance passed to it is misconfigured.fixEnsure your main application component is wrapped by `ConvexProviderWithAuth` (or its framework-specific equivalent from `convex-zen`), and that a properly initialized `ConvexReactClient` instance is passed as the `client` prop.
Warnings
- breaking Convex-Zen has strict peer dependency requirements for the `convex` package. Using an incompatible `convex` version can lead to runtime errors, breaking changes, or unexpected behavior in authentication flows.
- gotcha Framework-specific components and providers (e.g., `ConvexProviderWithAuth` for Next.js or Tanstack Start) are exposed through specific subpath exports. Importing them directly from the root `convex-zen` package will result in undefined symbols or module resolution failures.
- gotcha Proper setup requires defining `convex/zen.config.ts` and configuring `convex/auth.config.ts` to bridge with Convex-Zen. Furthermore, running `npx convex-zen generate` is crucial to process these configurations and generate necessary Convex functions and types. Skipping or misconfiguring these steps will lead to non-functional authentication.
- gotcha Convex-Zen is built primarily for modern JavaScript module environments (ESM). While bundlers generally handle this, direct usage or specific configurations in CommonJS-only projects might encounter module resolution issues.
Install
-
npm install convex-zen -
yarn add convex-zen -
pnpm add convex-zen
Imports
- defineConvexZen
const { defineConvexZen } = require('convex-zen');import { defineConvexZen } from 'convex-zen'; - ConvexProviderWithAuth
import { ConvexProviderWithAuth } from 'convex-zen'; import ConvexProviderWithAuth from 'convex-zen/tanstack-start';import { ConvexProviderWithAuth } from 'convex-zen/tanstack-start'; - useConvexAuth
import { useConvexAuth } from 'convex-zen/client'; // Incorrect subpath unless explicitly defined import { useConvexAuth } from 'convex-zen/next';import { useConvexAuth } from 'convex-zen';
Quickstart
npm install convex convex-zen @tanstack/react-start
// convex/zen.config.ts
import { defineConvexZen } from 'convex-zen';
export default defineConvexZen({
authProviders: [{
domain: process.env.NEXT_PUBLIC_CLERK_ISSUER_URL ?? '', // Example for Clerk or similar OIDC provider
appId: process.env.NEXT_PUBLIC_CLERK_APP_ID ?? '',
clientUrl: process.env.NEXT_PUBLIC_CONVEX_URL ?? '',
}],
// ... other configuration like roles, permissions
});
// convex/auth.config.ts
import * as auth from 'convex-zen/auth';
export default auth.config;
// app/root.tsx (Example for Tanstack Start application's root layout)
import React from 'react';
import { Outlet } from '@tanstack/react-router';
import { ConvexProviderWithAuth } from 'convex-zen/tanstack-start';
import { ConvexReactClient } from 'convex/react';
const convex = new ConvexReactClient(process.env.NEXT_PUBLIC_CONVEX_URL ?? '');
export default function Root() {
return (
<ConvexProviderWithAuth client={convex}>
<Outlet />
</ConvexProviderWithAuth>
);
}
// After defining configurations and installing packages, run the CLI tool:
npx convex-zen generate