Choosing Authentication — Agentuity Documentation

Choosing Authentication

Choose between Sign in with Agentuity and app-owned authentication for routes and apps

Choose the auth path based on who owns identity. For new apps that should let users sign in with their Agentuity account, start with Sign in with Agentuity. Use @agentuity/auth when you need app-owned sessions, API keys, or bearer tokens.

Which Auth Path Should I Use?

NeedUseStart Here
Users sign in with their Agentuity accountAgentuity OIDC providerSign in with Agentuity
Users grant scoped access to Agentuity resourcesAgentuity OIDC provider and OAuth scopesSign in with Agentuity
Your app owns users, sessions, API keys, or bearer tokens@agentuity/authQuick Start
Manage OAuth app credentials and scopesCLI or REST APICLI OAuth commands

Quick Start with @agentuity/auth

Install the auth and drizzle packages:

bun add @agentuity/auth @agentuity/drizzle

Create an auth instance, mount the auth routes, and protect your API with session middleware:

typescriptsrc/api/index.ts
import { Hono } from 'hono';
import type { Env } from '@agentuity/runtime';
import { createAuth, mountAuthRoutes, createSessionMiddleware } from '@agentuity/auth';
 
const auth = createAuth({
  connectionString: process.env.DATABASE_URL,
});
 
const router = new Hono<Env>();
 
// Expose sign-in, sign-up, session, and token endpoints
router.on(['GET', 'POST'], '/api/auth/*', mountAuthRoutes(auth));
 
// Require a valid session for all other API routes
router.use('/api/*', createSessionMiddleware(auth));
 
export default router;

Auth Methods

MethodWhen to UseHow It Works
SessionBrowser users with a login flowCookie-based, managed by BetterAuth
API KeyServer-to-server or CLI toolsHeader: x-agentuity-auth-api-key or Authorization: ApiKey <key>
Bearer TokenJWT-based service authHeader: Authorization: Bearer <token>

You can check which method authenticated a request with ctx.auth.authMethod, which returns 'session', 'api-key', or 'bearer'.

Auth in Different Contexts

Default Plugins

Agentuity Auth includes four plugins automatically:

PluginPurpose
organizationMulti-tenancy with teams, roles, and invitations
jwtJWT token generation with a JWKS endpoint
bearerBearer token auth via the Authorization header
apiKeyAPI key authentication for programmatic access

Database Setup

Agentuity Auth needs a PostgreSQL database to store users, sessions, and keys.

Connection string (simplest). Provide a URL and Agentuity creates the Drizzle adapter with the default resilient Postgres pool:

import { createAuth } from '@agentuity/auth';
 
const auth = createAuth({
  connectionString: process.env.DATABASE_URL,
});

Bring your own adapter. If you already have a Drizzle setup, pass it directly:

import { createAuth } from '@agentuity/auth';
import { createPostgresDrizzle, drizzleAdapter } from '@agentuity/drizzle';
import * as authSchema from '@agentuity/auth/schema';
import * as appSchema from './schema';
 
function requireEnv(name: string): string {
  const value = process.env[name];
  if (!value) {
    throw new Error(`${name} is required`);
  }
  return value;
}
 
const schema = { ...authSchema, ...appSchema };
const { db } = createPostgresDrizzle({
  connectionString: requireEnv('DATABASE_URL'),
  schema,
});
 
const auth = createAuth({
  database: drizzleAdapter(db, { provider: 'pg', schema: authSchema }),
});

Environment Variables

VariableDescriptionDefault
DATABASE_URLPostgreSQL connection stringRequired
AGENTUITY_AUTH_SECRETSecret for signing tokensFalls back to BETTER_AUTH_SECRET
AGENTUITY_CLOUD_BASE_URLPlatform-provided base URL for auth callbacksChecked before AGENTUITY_BASE_URL
AGENTUITY_BASE_URLBase URL for auth callbacksChecked before BETTER_AUTH_URL

By default, trusted origins include the resolved base URL, the incoming request origin, AGENTUITY_CLOUD_DOMAINS, and AUTH_TRUSTED_DOMAINS. AGENTUITY_BASE_URL is also trusted when set, even if AGENTUITY_CLOUD_BASE_URL supplied the resolved base URL. Passing trustedOrigins to createAuth() replaces that default list.

Sign in with Agentuity

Use the Agentuity OIDC provider when your app should redirect users to Agentuity, receive an authorization code, and create its own local app session from the returned profile. The provider handles identity, consent, OAuth apps, scopes, and tokens.

See Sign in with Agentuity for the route example, token storage pattern, and OAuth app setup.

Next Steps