@contentgrowth/content-auth 0.0.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 +255 -0
- package/dist/backend/index.d.ts +1077 -0
- package/dist/backend/index.js +15 -0
- package/dist/chunk-526JE62U.js +142 -0
- package/dist/chunk-NKDKDBM2.js +17 -0
- package/dist/chunk-R5U7XKVJ.js +16 -0
- package/dist/chunk-VX6RJ5XJ.js +372 -0
- package/dist/cli.d.ts +1 -0
- package/dist/cli.js +105 -0
- package/dist/frontend/client.d.ts +3161 -0
- package/dist/frontend/client.js +9 -0
- package/dist/frontend/index.d.ts +38 -0
- package/dist/frontend/index.js +19 -0
- package/dist/index.d.ts +12 -0
- package/dist/index.js +31 -0
- package/dist/styles.css +264 -0
- package/package.json +72 -0
- package/schema/auth.sql +115 -0
- package/schema/schema.template.ts +167 -0
package/README.md
ADDED
|
@@ -0,0 +1,255 @@
|
|
|
1
|
+
# @contentgrowth/content-auth
|
|
2
|
+
|
|
3
|
+
A wrapper around [Better Auth](https://better-auth.com) designed specifically for **Cloudflare Workers** and **Cloudflare Pages**, providing both backend Hono middleware and pre-built React frontend components.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- 🔐 **Secure Authentication**: Powered by Better Auth.
|
|
8
|
+
- ⚡ **Cloudflare Ready**: Optimized for Workers and Pages (D1 Database support).
|
|
9
|
+
- 🧩 **Hono Integration**: Easy middleware integration for your Hono API.
|
|
10
|
+
- ⚛️ **React Components**: Pre-built, customizable UI components for login/signup.
|
|
11
|
+
- 📦 **Type Safe**: Full TypeScript support.
|
|
12
|
+
|
|
13
|
+
## Installation
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
npm install @contentgrowth/content-auth
|
|
17
|
+
# or
|
|
18
|
+
yarn add @contentgrowth/content-auth
|
|
19
|
+
# or
|
|
20
|
+
pnpm add @contentgrowth/content-auth
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
## Usage
|
|
24
|
+
|
|
25
|
+
### 1. Backend Setup (Cloudflare Workers/Pages + Hono)
|
|
26
|
+
|
|
27
|
+
Create your authentication API using the `createAuthApp` helper. This sets up the necessary routes for Better Auth.
|
|
28
|
+
|
|
29
|
+
```typescript
|
|
30
|
+
// src/index.ts
|
|
31
|
+
import { Hono, createAuthApp } from '@contentgrowth/content-auth/backend';
|
|
32
|
+
|
|
33
|
+
type Bindings = {
|
|
34
|
+
DB: D1Database;
|
|
35
|
+
BETTER_AUTH_SECRET: string;
|
|
36
|
+
BASE_URL: string;
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
const app = new Hono<{ Bindings: Bindings }>();
|
|
40
|
+
|
|
41
|
+
app.mount('/api/auth', async (c) => {
|
|
42
|
+
// Initialize the auth app
|
|
43
|
+
const { app: authApp } = createAuthApp({
|
|
44
|
+
database: c.env.DB,
|
|
45
|
+
secret: c.env.BETTER_AUTH_SECRET,
|
|
46
|
+
baseUrl: c.env.BASE_URL, // e.g., http://localhost:5173 or https://your-app.pages.dev
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
return authApp.fetch(c.req.raw, c.env, c.executionCtx);
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
export default app;
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
#### Configuration Options (`createAuthApp`)
|
|
56
|
+
|
|
57
|
+
| Option | Type | Description |
|
|
58
|
+
| source | --- | --- |
|
|
59
|
+
| `database` | `D1Database` \| `any` | Your Cloudflare D1 binding or a Drizzle instance. |
|
|
60
|
+
| `secret` | `string` | A secret key for signing tokens. |
|
|
61
|
+
| `baseUrl` | `string` | The base URL of your application. |
|
|
62
|
+
| `provider` | `'sqlite'` \| `'postgres'` \| `'mysql'` | (Optional) Database provider. Defaults to `'sqlite'` (for D1). |
|
|
63
|
+
| `...` | `BetterAuthOptions` | Any other [Better Auth configuration](https://better-auth.com/docs/configuration) options. |
|
|
64
|
+
|
|
65
|
+
### Using without Hono (Standard Cloudflare Worker)
|
|
66
|
+
|
|
67
|
+
If you are not using Hono, you can use the `createAuth` function directly in your `fetch` handler.
|
|
68
|
+
|
|
69
|
+
```typescript
|
|
70
|
+
import { createAuth } from '@contentgrowth/content-auth/backend';
|
|
71
|
+
|
|
72
|
+
export default {
|
|
73
|
+
async fetch(request, env, ctx) {
|
|
74
|
+
const auth = createAuth({
|
|
75
|
+
database: env.DB,
|
|
76
|
+
secret: env.BETTER_AUTH_SECRET,
|
|
77
|
+
baseUrl: env.BASE_URL
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
// Manually handle the auth routes
|
|
81
|
+
if (request.url.includes("/api/auth")) {
|
|
82
|
+
return auth.handler(request);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
return new Response("Hello World");
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
### Using with Other Databases (Postgres/MySQL)
|
|
91
|
+
|
|
92
|
+
You can use this package with any database supported by Drizzle ORM (Postgres, MySQL, etc.) on any platform (Node.js, Bun, etc.).
|
|
93
|
+
|
|
94
|
+
1. Initialize your Drizzle instance.
|
|
95
|
+
2. Pass it to `createAuthApp`.
|
|
96
|
+
3. Set the `provider` option.
|
|
97
|
+
|
|
98
|
+
```typescript
|
|
99
|
+
import { Hono } from 'hono';
|
|
100
|
+
import { createAuthApp } from '@contentgrowth/content-auth/backend';
|
|
101
|
+
import { drizzle } from 'drizzle-orm/node-postgres';
|
|
102
|
+
import { Client } from 'pg';
|
|
103
|
+
|
|
104
|
+
const client = new Client({
|
|
105
|
+
connectionString: process.env.DATABASE_URL,
|
|
106
|
+
});
|
|
107
|
+
await client.connect();
|
|
108
|
+
const db = drizzle(client);
|
|
109
|
+
|
|
110
|
+
const app = new Hono();
|
|
111
|
+
|
|
112
|
+
const { app: authApp } = createAuthApp({
|
|
113
|
+
database: db,
|
|
114
|
+
secret: process.env.BETTER_AUTH_SECRET,
|
|
115
|
+
baseUrl: process.env.BASE_URL,
|
|
116
|
+
provider: "postgres", // or "mysql"
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
app.route('/api/auth', authApp);
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
### 2. Frontend Setup (React)
|
|
123
|
+
|
|
124
|
+
Use the provided `AuthForm` component to render a sign-in/sign-up form.
|
|
125
|
+
|
|
126
|
+
```tsx
|
|
127
|
+
// src/App.tsx
|
|
128
|
+
import React from 'react';
|
|
129
|
+
import { AuthForm } from '@contentgrowth/content-auth/frontend';
|
|
130
|
+
import '@contentgrowth/content-auth/styles.css'; // Import default styles
|
|
131
|
+
|
|
132
|
+
export default function App() {
|
|
133
|
+
return (
|
|
134
|
+
<div className="min-h-screen flex items-center justify-center bg-gray-50">
|
|
135
|
+
<div className="max-w-md w-full">
|
|
136
|
+
<h1 className="text-2xl font-bold text-center mb-6">Welcome Back</h1>
|
|
137
|
+
|
|
138
|
+
<AuthForm
|
|
139
|
+
type="signin" // or "signup"
|
|
140
|
+
onSuccess={() => {
|
|
141
|
+
console.log('User authenticated!');
|
|
142
|
+
window.location.href = '/dashboard';
|
|
143
|
+
}}
|
|
144
|
+
/>
|
|
145
|
+
</div>
|
|
146
|
+
</div>
|
|
147
|
+
);
|
|
148
|
+
}
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
#### Client Client
|
|
152
|
+
|
|
153
|
+
You can also use the `authClient` directly for custom implementations:
|
|
154
|
+
|
|
155
|
+
```typescript
|
|
156
|
+
import { authClient } from '@contentgrowth/content-auth/client';
|
|
157
|
+
|
|
158
|
+
// Example: Sign in with email
|
|
159
|
+
await authClient.signIn.email({
|
|
160
|
+
email: "user@example.com",
|
|
161
|
+
password: "password123"
|
|
162
|
+
});
|
|
163
|
+
```
|
|
164
|
+
|
|
165
|
+
## Database Setup
|
|
166
|
+
|
|
167
|
+
This package requires specific database tables to function. We provide a **SQL schema template** that you copy into your project and extend with your own tables.
|
|
168
|
+
|
|
169
|
+
### Step 1: Initialize the Schema
|
|
170
|
+
|
|
171
|
+
Use the CLI to initialize the schema in your project:
|
|
172
|
+
|
|
173
|
+
```bash
|
|
174
|
+
npx content-auth init
|
|
175
|
+
```
|
|
176
|
+
|
|
177
|
+
This creates `./migrations/0000_auth.sql` with all required auth tables.
|
|
178
|
+
|
|
179
|
+
**Options:**
|
|
180
|
+
- `-o, --output <path>` — Custom output path (default: `./migrations/0000_auth.sql`)
|
|
181
|
+
- `--force` — Overwrite existing file
|
|
182
|
+
|
|
183
|
+
```bash
|
|
184
|
+
# Example: Custom output path
|
|
185
|
+
npx content-auth init -o ./db/migrations/0001_auth.sql
|
|
186
|
+
```
|
|
187
|
+
|
|
188
|
+
### Step 2: Add Your Application Tables
|
|
189
|
+
|
|
190
|
+
Edit the copied file and add your application-specific tables at the bottom:
|
|
191
|
+
|
|
192
|
+
```sql
|
|
193
|
+
-- ... (auth tables from template above) ...
|
|
194
|
+
|
|
195
|
+
-- ==========================================
|
|
196
|
+
-- YOUR APPLICATION TABLES
|
|
197
|
+
-- ==========================================
|
|
198
|
+
|
|
199
|
+
CREATE TABLE IF NOT EXISTS my_entity (
|
|
200
|
+
id TEXT PRIMARY KEY,
|
|
201
|
+
org_id TEXT NOT NULL, -- References organization.id
|
|
202
|
+
name TEXT NOT NULL,
|
|
203
|
+
created_at INTEGER
|
|
204
|
+
);
|
|
205
|
+
```
|
|
206
|
+
|
|
207
|
+
### Step 3: Run Migrations
|
|
208
|
+
|
|
209
|
+
```bash
|
|
210
|
+
# For Cloudflare D1 (local)
|
|
211
|
+
wrangler d1 migrations apply DB --local
|
|
212
|
+
|
|
213
|
+
# For Cloudflare D1 (remote)
|
|
214
|
+
wrangler d1 migrations apply DB --remote
|
|
215
|
+
```
|
|
216
|
+
|
|
217
|
+
### Extending Auth Tables
|
|
218
|
+
|
|
219
|
+
You can add extra columns to any auth table for your business needs. Just ensure you **keep all existing columns** — they are required by Better Auth.
|
|
220
|
+
|
|
221
|
+
```sql
|
|
222
|
+
-- Example: Adding custom fields to organization
|
|
223
|
+
CREATE TABLE IF NOT EXISTS organization (
|
|
224
|
+
-- Required columns (keep these)
|
|
225
|
+
id TEXT PRIMARY KEY,
|
|
226
|
+
name TEXT NOT NULL,
|
|
227
|
+
slug TEXT UNIQUE,
|
|
228
|
+
logo TEXT,
|
|
229
|
+
createdAt TIMESTAMP NOT NULL,
|
|
230
|
+
metadata TEXT,
|
|
231
|
+
|
|
232
|
+
-- Your custom columns
|
|
233
|
+
domain TEXT,
|
|
234
|
+
is_verified BOOLEAN DEFAULT FALSE,
|
|
235
|
+
billing_tier TEXT DEFAULT 'free'
|
|
236
|
+
);
|
|
237
|
+
```
|
|
238
|
+
|
|
239
|
+
### Schema Reference
|
|
240
|
+
|
|
241
|
+
| Table | Purpose |
|
|
242
|
+
|-------|---------|
|
|
243
|
+
| `user` | User accounts |
|
|
244
|
+
| `session` | Active sessions |
|
|
245
|
+
| `account` | OAuth/credential providers |
|
|
246
|
+
| `verification` | Email/token verification |
|
|
247
|
+
| `organization` | Teams/orgs (org plugin) |
|
|
248
|
+
| `member` | Org membership (org plugin) |
|
|
249
|
+
| `invitation` | Pending invites (org plugin) |
|
|
250
|
+
|
|
251
|
+
For detailed field definitions, see `schema/auth.sql` or the [Better Auth Drizzle Adapter documentation](https://better-auth.com/docs/adapters/drizzle).
|
|
252
|
+
|
|
253
|
+
## License
|
|
254
|
+
|
|
255
|
+
MIT
|