Loading...
;
+ if (error) return
-If no ORM is detected, `init` prompts to run migrations. If you're using Drizzle or Prisma, run migrations manually with your ORM tools. Use `--skipMigrations` to skip the prompt, then run `agentuity project auth generate` to get the SQL schema.
+
+If you're using a development secret, generate a new one before deploying to production. Store it securely in your environment variables.
-### Database Configuration
+## Database Configuration
-**Connection String (simplest):**
+### Connection String (Simplest)
+
+Just provide the connection string and Agentuity handles the rest:
```typescript
import { createAuth } from '@agentuity/auth';
@@ -174,7 +404,9 @@ export const auth = createAuth({
});
```
-**Bring Your Own Drizzle:**
+### Bring Your Own Drizzle
+
+If you have an existing Drizzle setup:
```typescript
import { drizzle } from 'drizzle-orm/bun-sql';
@@ -190,75 +422,97 @@ export const auth = createAuth({
});
```
----
-
-## Common Patterns
+The `@agentuity/auth/schema` export provides all auth-related Drizzle tables (`user`, `session`, `account`, `verification`, `organization`, `member`, `invitation`, `apiKey`).
-These patterns work with any auth provider.
+## Built-in Features
-### Using Auth State in Components
+### Organizations & Teams
-```tsx
-import { useAPI, useAgentuity } from '@agentuity/react';
+Create and manage organizations:
-function Dashboard() {
- const { isAuthenticated, authLoading } = useAgentuity();
- const { data, refetch } = useAPI('GET /api/profile');
+```typescript
+// Create an organization
+const org = await auth.api.createOrganization({
+ body: { name: 'My Team', slug: 'my-team' },
+ headers: c.req.raw.headers,
+});
- if (authLoading) return Loading...
;
- if (!isAuthenticated) return Please sign in
;
+// Get user's role in active org
+const role = await c.var.auth.getOrgRole();
- return (
-
-
Welcome, {data?.name}
-
-
- );
+// Check role
+if (await c.var.auth.hasOrgRole('admin', 'owner')) {
+ // Admin actions
}
```
-### Global Route Protection
+
+See the [BetterAuth Organization Plugin docs](https://www.better-auth.com/docs/plugins/organization) for the complete API including invitations, member management, and role configuration.
+
+
+### API Keys
+
+Create API keys for programmatic access:
```typescript
-import { createRouter } from '@agentuity/runtime';
-import { authMiddleware } from '../auth';
+const result = await auth.api.createApiKey({
+ body: {
+ name: 'my-integration',
+ userId: user.id,
+ expiresIn: 60 * 60 * 24 * 30, // 30 days
+ permissions: { project: ['read', 'write'] },
+ },
+});
+console.log('API Key:', result.key); // Only shown once!
+```
-const router = createRouter();
+
+See the [BetterAuth API Key Plugin docs](https://www.better-auth.com/docs/plugins/api-key) for listing, revoking, and permission schemas.
+
-// Protect all /api/* routes
-router.use('/api/*', authMiddleware);
+### JWT & Bearer Tokens
-router.get('/api/profile', async (c) => {
- const user = await c.var.auth.getUser();
- return c.json({ email: user.email });
-});
+```typescript
+// Get token in route handler
+const token = await c.var.auth.getToken();
-export default router;
+// JWKS endpoint: GET /api/auth/.well-known/jwks.json
```
-### Optional Authentication
+
+See the [BetterAuth JWT Plugin docs](https://www.better-auth.com/docs/plugins/jwt) for token customization and verification.
+
-Allow both authenticated and anonymous access:
+## CLI Commands
-```typescript
-import { createSessionMiddleware } from '@agentuity/auth';
-import { auth } from '../auth';
+### Initialize Auth
-const optionalAuth = createSessionMiddleware(auth, { optional: true });
+```bash
+agentuity project auth init [options]
+```
-router.get('/api/content', optionalAuth, async (c) => {
- const user = await c.var.auth.getUser();
+| Option | Description |
+|--------|-------------|
+| `--skipMigrations` | Skip running database migrations |
+| `--skipInstall` | Skip installing dependencies |
+
+
+If Drizzle or Prisma is detected in your project, `init` will skip automatic migrations and prompt you to run them with your ORM tools instead.
+
- if (user) {
- return c.json({ content: 'Premium content', userId: user.id });
- }
+### Generate Schema
- return c.json({ content: 'Public content' });
-});
+Generate SQL schema for auth tables (useful for manual migrations):
+
+```bash
+agentuity project auth generate
```
+This outputs the SQL needed to create all auth tables, which you can then run with your preferred migration tool.
+
## Next Steps
-- [Middleware & Authentication](/Routes/middleware): More middleware patterns
-- [Provider Setup](/Frontend/provider-setup): AgentuityProvider configuration
-- [React Hooks](/Frontend/react-hooks): Building custom UIs
+- [Middleware & Routes](/Routes/middleware) - More middleware patterns
+- [Provider Setup](/Frontend/provider-setup) - AgentuityProvider configuration
+- [React Hooks](/Frontend/react-hooks) - Building custom UIs
+- [Auth Testing App](https://github.com/agentuity/sdk/tree/main/apps/testing/auth-package-app) - Complete working examples