AI Rules are in Beta
AI Rules are currently in beta. We're actively improving them and would love to hear your feedback. Join us on Discord to share your experience and suggestions.
Related docs
Repository
How to use
You can use these rules in two ways:
Option 1: Copy from this page
With Cursor, save the rules to
.cursor/rules/neon-auth.mdcand they'll be automatically applied when working with matching files (*.ts,*.tsx).For other AI tools, you can include these rules as context when chatting with your AI assistant - check your tool's documentation for the specific method (like using "Include file" or context commands).
Option 2: Clone from repository
If you prefer, you can clone or download the rules directly from our AI Rules repository.
Once added to your project, AI tools will automatically use these rules when working with Neon Auth code. You can also reference them explicitly in prompts.
Rules
---
description: Use these rules when implementing authentication with @neondatabase/auth or @neondatabase/neon-js
globs: *.tsx, *.ts
alwaysApply: false
---
# Neon Auth Guidelines
## Overview
Neon Auth provides authentication for your application. It's available as:
- `@neondatabase/auth` - Auth only (smaller bundle)
- `@neondatabase/neon-js` - Auth + Data API (full SDK)
## Package Selection
| Need | Package | Bundle |
|------|---------|--------|
| Auth only | `@neondatabase/auth` | Smaller |
| Auth + Database queries | `@neondatabase/neon-js` | Full |
## Installation
```bash
# Auth only
npm install @neondatabase/auth
# Auth + Data API
npm install @neondatabase/neon-js
```
## Quick Setup Patterns
### Next.js App Router
**1. API Route Handler:**
```typescript
// app/api/auth/[...path]/route.ts
import { authApiHandler } from "@neondatabase/auth/next";
export const { GET, POST } = authApiHandler();
```
**2. Auth Client:**
```typescript
// lib/auth/client.ts
import { createAuthClient } from "@neondatabase/auth/next";
export const authClient = createAuthClient();
```
**3. Use in Components:**
```typescript
"use client";
import { authClient } from "@/lib/auth/client";
function AuthStatus() {
const session = authClient.useSession();
if (session.isPending) return <div>Loading...</div>;
if (!session.data) return <SignInButton />;
return <div>Hello, {session.data.user.name}</div>;
}
```
**Complete setup:** See [Setup Reference](https://raw.githubusercontent.com/neondatabase-labs/ai-rules/main/references/neon-auth-setup.md#nextjs-app-router)
### React SPA
```typescript
import { createAuthClient } from "@neondatabase/auth";
import { BetterAuthReactAdapter } from "@neondatabase/auth/react/adapters";
const authClient = createAuthClient(
import.meta.env.VITE_NEON_AUTH_URL,
{ adapter: BetterAuthReactAdapter() }
);
```
**Complete setup with router:** See [Setup Reference](https://raw.githubusercontent.com/neondatabase-labs/ai-rules/main/references/neon-auth-setup.md#react-spa)
### Node.js Backend
```typescript
import { createAuthClient } from "@neondatabase/auth";
const auth = createAuthClient(process.env.NEON_AUTH_URL!);
await auth.signIn.email({ email, password });
const session = await auth.getSession();
```
**Complete setup:** See [Setup Reference](https://raw.githubusercontent.com/neondatabase-labs/ai-rules/main/references/neon-auth-setup.md#nodejs-backend)
## Environment Variables
```bash
# Next.js (.env.local)
NEON_AUTH_BASE_URL=https://ep-xxx.neonauth.c-2.us-east-2.aws.neon.build/dbname/auth
NEXT_PUBLIC_NEON_AUTH_URL=https://ep-xxx.neonauth.c-2.us-east-2.aws.neon.build/dbname/auth
# Vite/React (.env)
VITE_NEON_AUTH_URL=https://ep-xxx.neonauth.c-2.us-east-2.aws.neon.build/dbname/auth
```
**Complete reference:** See [Setup Reference](https://raw.githubusercontent.com/neondatabase-labs/ai-rules/main/references/neon-auth-setup.md#environment-variables)
## Auth Methods
### Sign Up
```typescript
await auth.signUp.email({
email: "user@example.com",
password: "securepassword",
name: "John Doe", // Optional
});
```
### Sign In
```typescript
// Email/password
await auth.signIn.email({
email: "user@example.com",
password: "securepassword",
});
// Social (Google, GitHub)
await auth.signIn.social({
provider: "google", // or "github"
callbackURL: "/dashboard",
});
```
### Sign Out
```typescript
await auth.signOut();
```
### Get Session
```typescript
// Async (Node.js, server components)
const session = await auth.getSession();
// React hook (client components)
const session = auth.useSession();
// Returns: { data: Session | null, isPending: boolean }
```
### Session Data Structure
```typescript
interface Session {
user: {
id: string;
name: string | null;
email: string;
image: string | null;
emailVerified: boolean;
createdAt: Date;
updatedAt: Date;
};
session: {
id: string;
expiresAt: Date;
token: string;
createdAt: Date;
updatedAt: Date;
userId: string;
};
}
```
## UI Components
### Quick Pattern (Next.js)
```typescript
// 1. Import CSS
import "@neondatabase/auth/ui/css";
// 2. Create provider: app/auth-provider.tsx
"use client";
import { NeonAuthUIProvider } from "@neondatabase/auth/react/ui";
import { authClient } from "@/lib/auth/client";
import { useRouter } from "next/navigation";
import Link from "next/link";
export function AuthProvider({ children }: { children: React.ReactNode }) {
const router = useRouter();
return (
<NeonAuthUIProvider
authClient={authClient}
navigate={router.push}
replace={router.replace}
onSessionChange={() => router.refresh()}
Link={Link}
>
{children}
</NeonAuthUIProvider>
);
}
```
**Complete UI reference:** See [UI Components Reference](https://raw.githubusercontent.com/neondatabase-labs/ai-rules/main/references/neon-auth-ui.md)
## React Styling Guidelines
**CRITICAL:** When creating React components, always use existing CSS variables. Do NOT create new colors or style values.
### Use Existing CSS Variables
The UI package provides all necessary CSS variables. Always use these variables in your custom components (navbar, layouts, pages, etc.):
| Variable | Purpose | Usage |
|----------|---------|-------|
| `--background`, `--foreground` | Page background/text | `hsl(var(--background))` |
| `--card`, `--card-foreground` | Card surfaces | `hsl(var(--card))` |
| `--primary`, `--primary-foreground` | Primary buttons/actions | `hsl(var(--primary))` |
| `--secondary`, `--secondary-foreground` | Secondary elements | `hsl(var(--secondary))` |
| `--muted`, `--muted-foreground` | Muted/subtle elements | `hsl(var(--muted))` |
| `--destructive` | Destructive/danger actions | `hsl(var(--destructive))` |
| `--border`, `--input`, `--ring` | Borders and focus rings | `hsl(var(--border))` |
| `--radius` | Border radius | `var(--radius)` |
### Examples
**✅ Correct - Uses CSS variables:**
```tsx
<div style={{ background: 'hsl(var(--background))', color: 'hsl(var(--foreground))' }}>
<button style={{ background: 'hsl(var(--primary))', color: 'hsl(var(--primary-foreground))' }}>
Submit
</button>
</div>
```
**❌ Wrong - Creates new colors:**
```tsx
<div style={{ background: '#ffffff', color: '#000000' }}>
<button style={{ background: '#3b82f6', color: '#ffffff' }}>
Submit
</button>
</div>
```
**Why:** Using CSS variables ensures:
- Visual consistency with auth components
- Automatic dark mode support
- Proper theming integration
- No duplicate color definitions
**Complete theming guide:** See [UI Theming Guide](https://raw.githubusercontent.com/neondatabase-labs/ai-rules/main/references/neon-js-theming.md)
## Key Imports
```typescript
// Auth client (Next.js)
import { authApiHandler, createAuthClient } from "@neondatabase/auth/next";
// Auth client (vanilla)
import { createAuthClient } from "@neondatabase/auth";
// React adapter (NOT from main entry)
import { BetterAuthReactAdapter } from "@neondatabase/auth/react/adapters";
// UI components
import { NeonAuthUIProvider, AuthView, SignInForm } from "@neondatabase/auth/react/ui";
import { authViewPaths } from "@neondatabase/auth/react/ui/server";
// CSS
import "@neondatabase/auth/ui/css";
```
**Complete import reference:** See [Import Reference](https://raw.githubusercontent.com/neondatabase-labs/ai-rules/main/references/neon-js-imports.md)
## Common Mistakes
> **Full Reference:** See [Common Mistakes Guide](https://raw.githubusercontent.com/neondatabase-labs/ai-rules/main/references/neon-auth-common-mistakes.md) for detailed solutions.
**Quick Summary:**
1. **Wrong adapter import**: Import `BetterAuthReactAdapter` from `auth/react/adapters` subpath
2. **Forgetting to call adapter**: Use `BetterAuthReactAdapter()` with parentheses
3. **Missing CSS**: Import from `ui/css` or `ui/tailwind` (not both)
4. **Missing "use client"**: Required for components using `useSession()`
5. **Wrong createAuthClient signature**: First arg is URL: `createAuthClient(url, { adapter })`
**Code generation rules:** See [Code Generation Rules](https://raw.githubusercontent.com/neondatabase-labs/ai-rules/main/references/code-generation-rules.md)
## Error Handling
```typescript
const { error } = await auth.signIn.email({ email, password });
if (error) {
switch (error.code) {
case "INVALID_EMAIL_OR_PASSWORD":
showError("Invalid email or password");
break;
case "EMAIL_NOT_VERIFIED":
showError("Please verify your email");
break;
case "USER_NOT_FOUND":
showError("User not found");
break;
case "TOO_MANY_REQUESTS":
showError("Too many attempts. Please wait.");
break;
default:
showError("Authentication failed");
}
}
```
## References
- [Setup Reference](https://raw.githubusercontent.com/neondatabase-labs/ai-rules/main/references/neon-auth-setup.md) - Complete setup guide for all frameworks
- [UI Components Reference](https://raw.githubusercontent.com/neondatabase-labs/ai-rules/main/references/neon-auth-ui.md) - All UI components and provider configuration
- [Common Mistakes](https://raw.githubusercontent.com/neondatabase-labs/ai-rules/main/references/neon-auth-common-mistakes.md) - Import paths, adapter patterns, CSS
- [Troubleshooting Guide](https://raw.githubusercontent.com/neondatabase-labs/ai-rules/main/references/neon-auth-troubleshooting.md) - Error solutions
- [Code Generation Rules](https://raw.githubusercontent.com/neondatabase-labs/ai-rules/main/references/code-generation-rules.md) - Import and CSS strategies
- [Auth Adapters Guide](https://raw.githubusercontent.com/neondatabase-labs/ai-rules/main/references/neon-js-adapters.md) - Adapter comparison
- [Import Reference](https://raw.githubusercontent.com/neondatabase-labs/ai-rules/main/references/neon-js-imports.md) - Complete import paths
- [UI Theming Guide](https://raw.githubusercontent.com/neondatabase-labs/ai-rules/main/references/neon-js-theming.md) - Styling options
- For database queries with auth, see **neon-js.mdc**