@neondatabase/auth 0.1.0-beta.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,260 @@
1
+ # @neondatabase/auth
2
+
3
+ [![npm version](https://img.shields.io/npm/v/@neondatabase/auth.svg)](https://www.npmjs.com/package/@neondatabase/auth)
4
+ [![npm downloads](https://img.shields.io/npm/dm/@neondatabase/auth.svg)](https://www.npmjs.com/package/@neondatabase/auth)
5
+ [![TypeScript](https://img.shields.io/badge/TypeScript-5.0%2B-blue.svg)](https://www.typescriptlang.org/)
6
+ [![License](https://img.shields.io/npm/l/@neondatabase/auth.svg)](https://github.com/neondatabase-labs/neon-js/blob/main/LICENSE)
7
+
8
+ Authentication adapters for Neon Auth, supporting multiple auth providers.
9
+
10
+ ## Overview
11
+
12
+ `@neondatabase/auth` provides authentication for applications using Neon Auth. By default, it uses the Better Auth API, with optional adapters for different API styles:
13
+
14
+ - **Default** - Better Auth API (`signIn.email`, `signUp.email`, etc.)
15
+ - **SupabaseAuthAdapter** - Supabase-compatible API for migrations (`signInWithPassword`, `signUp`, etc.)
16
+ - **BetterAuthReactAdapter** - Better Auth with React hooks (`useSession`)
17
+
18
+ This package is designed to work seamlessly with Neon's authentication infrastructure while providing:
19
+
20
+ - **Simple default API** - Works out of the box with Better Auth patterns
21
+ - **Optional adapters** - Switch API styles for migrations or preferences
22
+ - **Performance optimizations** - Session caching and request deduplication
23
+ - **TypeScript support** - Fully typed with strict type checking
24
+
25
+ ## Installation
26
+
27
+ ```bash
28
+ npm install @neondatabase/auth
29
+ # or
30
+ bun add @neondatabase/auth
31
+ ```
32
+
33
+ ## Usage
34
+
35
+ ### Basic Usage (Default)
36
+
37
+ The `createAuthClient` factory function creates an auth client. By default, it uses the Better Auth API:
38
+
39
+ ```typescript
40
+ import { createAuthClient } from '@neondatabase/auth';
41
+
42
+ const auth = createAuthClient('https://your-auth-server.com');
43
+
44
+ // Sign up
45
+ await auth.signUp.email({
46
+ email: 'user@example.com',
47
+ password: 'secure-password',
48
+ name: 'John Doe',
49
+ });
50
+
51
+ // Sign in
52
+ await auth.signIn.email({
53
+ email: 'user@example.com',
54
+ password: 'secure-password',
55
+ });
56
+
57
+ // Get session
58
+ const session = await auth.getSession();
59
+
60
+ // Sign out
61
+ await auth.signOut();
62
+ ```
63
+
64
+ ### OAuth Authentication
65
+
66
+ ```typescript
67
+ import { createAuthClient } from '@neondatabase/auth';
68
+
69
+ const auth = createAuthClient('https://your-auth-server.com');
70
+
71
+ await auth.signIn.social({
72
+ provider: 'google',
73
+ callbackURL: '/dashboard',
74
+ });
75
+ ```
76
+
77
+ ## Using Adapters
78
+
79
+ You can optionally specify an adapter to change the API style. This is useful for migrations or if you prefer a different API.
80
+
81
+ ### SupabaseAuthAdapter - Supabase-compatible API
82
+
83
+ Use this adapter if you're migrating from Supabase or prefer the Supabase API style:
84
+
85
+ ```typescript
86
+ import { createAuthClient, SupabaseAuthAdapter } from '@neondatabase/auth';
87
+
88
+ const auth = createAuthClient('https://your-auth-server.com', {
89
+ adapter: SupabaseAuthAdapter(),
90
+ });
91
+
92
+ // Supabase-compatible methods
93
+ await auth.signUp({
94
+ email: 'user@example.com',
95
+ password: 'secure-password',
96
+ options: {
97
+ data: { name: 'John Doe' },
98
+ },
99
+ });
100
+
101
+ await auth.signInWithPassword({
102
+ email: 'user@example.com',
103
+ password: 'secure-password',
104
+ });
105
+
106
+ const { data: session } = await auth.getSession();
107
+ await auth.signOut();
108
+
109
+ // OAuth with Supabase-style API
110
+ await auth.signInWithOAuth({
111
+ provider: 'google',
112
+ options: {
113
+ redirectTo: '/dashboard',
114
+ },
115
+ });
116
+ ```
117
+
118
+ ### BetterAuthReactAdapter - React Hooks Support
119
+
120
+ Use this adapter in React applications to get access to hooks like `useSession`:
121
+
122
+ ```typescript
123
+ import { createAuthClient } from '@neondatabase/auth';
124
+ import { BetterAuthReactAdapter } from '@neondatabase/auth/react/adapters';
125
+
126
+ const auth = createAuthClient('https://your-auth-server.com', {
127
+ adapter: BetterAuthReactAdapter(),
128
+ });
129
+
130
+ // Same API as default
131
+ await auth.signIn.email({
132
+ email: 'user@example.com',
133
+ password: 'secure-password',
134
+ });
135
+
136
+ // Plus React hooks
137
+ function MyComponent() {
138
+ const session = auth.useSession();
139
+
140
+ if (session.isPending) return <div>Loading...</div>;
141
+ if (!session.data) return <div>Not logged in</div>;
142
+
143
+ return <div>Hello, {session.data.user.name}</div>;
144
+ }
145
+ ```
146
+
147
+ ## API Reference
148
+
149
+ ### createAuthClient(url, config?)
150
+
151
+ Factory function to create an auth client.
152
+
153
+ **Parameters:**
154
+ - `url` - The auth service URL (required)
155
+ - `config.adapter` - Optional adapter factory function (e.g., `SupabaseAuthAdapter()`)
156
+
157
+ **Returns:** The adapter's public API (varies by adapter type)
158
+
159
+ ### Default API (Better Auth)
160
+
161
+ - `signIn.email(credentials)` - Sign in with email
162
+ - `signIn.social(options)` - Sign in with OAuth
163
+ - `signUp.email(credentials)` - Create new user
164
+ - `signOut()` - Sign out current user
165
+ - `getSession()` - Get current session
166
+
167
+ ### SupabaseAuthAdapter API
168
+
169
+ Provides a Supabase-compatible API:
170
+
171
+ - `signUp(credentials)` - Create a new user
172
+ - `signInWithPassword(credentials)` - Sign in with email/password
173
+ - `signInWithOAuth(options)` - Sign in with OAuth provider
174
+ - `signOut()` - Sign out current user
175
+ - `getSession()` - Get current session
176
+ - `getUser()` - Get current user
177
+ - `updateUser(attributes)` - Update user metadata
178
+ - `getUserIdentities()` - Get linked OAuth identities
179
+ - `linkIdentity(credentials)` - Link OAuth provider
180
+ - `unlinkIdentity(identity)` - Unlink OAuth provider
181
+ - `resetPasswordForEmail(email, options)` - Send password reset
182
+ - `onAuthStateChange(callback)` - Listen to auth state changes
183
+
184
+ ### BetterAuthReactAdapter API
185
+
186
+ Same as default API, plus:
187
+
188
+ - `useSession()` - React hook for session state
189
+
190
+ ## Performance Features
191
+
192
+ ### Session Caching
193
+
194
+ Sessions are cached in memory with intelligent TTL management:
195
+ - 60-second default cache TTL
196
+ - Automatic expiration based on JWT `exp` claim
197
+ - Lazy expiration checking on reads
198
+ - Synchronous cache clearing on sign-out
199
+
200
+ ### Request Deduplication
201
+
202
+ Multiple concurrent `getSession()` calls are automatically deduplicated:
203
+ - Single network request for concurrent calls
204
+ - 10x faster cold starts (10 concurrent calls: ~2000ms → ~200ms)
205
+ - Reduces server load by N-1 for N concurrent calls
206
+
207
+ ## Environment Compatibility
208
+
209
+ - Node.js 14+
210
+ - Browser (all modern browsers)
211
+ - Edge Runtime (Vercel, Cloudflare Workers, etc.)
212
+ - Bun
213
+
214
+ ## Next.js Integration
215
+
216
+ For Next.js projects, this package provides built-in integration via `@neondatabase/auth/next`. See the [Next.js Setup Guide](./NEXT-JS.md) for:
217
+
218
+ - Creating API route handlers with `toNextJsHandler()`
219
+ - Setting up the auth client for client components
220
+ - Configuring the `NeonAuthUIProvider`
221
+ - Importing styles (with or without Tailwind CSS)
222
+
223
+ ## CSS for UI Components
224
+
225
+ If you're using `@neondatabase/auth-ui` components, CSS is conveniently re-exported from this package:
226
+
227
+ | Export | Use Case |
228
+ |--------|----------|
229
+ | `@neondatabase/auth/ui/css` | Pre-built styles (~47KB) |
230
+ | `@neondatabase/auth/ui/tailwind` | Tailwind-ready CSS |
231
+
232
+ ```css
233
+ /* Without Tailwind */
234
+ @import '@neondatabase/auth/ui/css';
235
+
236
+ /* With Tailwind CSS v4 */
237
+ @import 'tailwindcss';
238
+ @import '@neondatabase/auth/ui/tailwind';
239
+ ```
240
+
241
+ ## Related Packages
242
+
243
+ - [`@neondatabase/neon-js`](../neon-js) - Full SDK with database and auth integration
244
+ - [`@neondatabase/postgrest-js`](../postgrest-js) - PostgreSQL client without auth
245
+ - [`@neondatabase/auth-ui`](../auth-ui) - UI components for Neon Auth
246
+
247
+ ## Resources
248
+
249
+ - [Neon Auth Documentation](https://neon.tech/docs/neon-auth)
250
+ - [Better Auth Documentation](https://www.better-auth.com/docs)
251
+ - [Supabase Auth Reference](https://supabase.com/docs/reference/javascript/auth-signup)
252
+
253
+ ## Support
254
+
255
+ - [GitHub Issues](https://github.com/neondatabase/neon-js/issues)
256
+ - [Neon Community Discord](https://discord.gg/H24eC2UN)
257
+
258
+ ## License
259
+
260
+ Apache-2.0