@saas-maker/auth-preset 0.1.0
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 +95 -0
- package/dist/client.d.mts +1648 -0
- package/dist/client.d.ts +1648 -0
- package/dist/client.js +67 -0
- package/dist/client.js.map +1 -0
- package/dist/client.mjs +39 -0
- package/dist/client.mjs.map +1 -0
- package/dist/index.d.mts +105 -0
- package/dist/index.d.ts +105 -0
- package/dist/index.js +92 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +66 -0
- package/dist/index.mjs.map +1 -0
- package/dist/next.d.mts +25 -0
- package/dist/next.d.ts +25 -0
- package/dist/next.js +34 -0
- package/dist/next.js.map +1 -0
- package/dist/next.mjs +9 -0
- package/dist/next.mjs.map +1 -0
- package/package.json +57 -0
package/README.md
ADDED
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
# @saas-maker/auth-preset
|
|
2
|
+
|
|
3
|
+
Foundry-standard wrapper around [better-auth](https://better-auth.com). Bakes in Google provider, secure session cookies, and the D1 adapter so every Foundry app gets the same auth posture in two lines.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
pnpm add @saas-maker/auth-preset better-auth drizzle-orm
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## .env.example
|
|
12
|
+
|
|
13
|
+
```bash
|
|
14
|
+
BETTER_AUTH_SECRET=<openssl rand -base64 32>
|
|
15
|
+
AUTH_URL=https://app.example.com
|
|
16
|
+
GOOGLE_CLIENT_ID=
|
|
17
|
+
GOOGLE_CLIENT_SECRET=
|
|
18
|
+
NODE_ENV=production
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
## Server — Cloudflare Workers / OpenNext
|
|
22
|
+
|
|
23
|
+
```ts
|
|
24
|
+
// lib/auth.ts
|
|
25
|
+
import { createAuth } from '@saas-maker/auth-preset';
|
|
26
|
+
import { getCloudflareContext } from '@opennextjs/cloudflare';
|
|
27
|
+
import * as schema from './auth-schema';
|
|
28
|
+
|
|
29
|
+
let _auth: ReturnType<typeof createAuth> | null = null;
|
|
30
|
+
|
|
31
|
+
export function getAuth() {
|
|
32
|
+
if (_auth) return _auth;
|
|
33
|
+
const { env } = getCloudflareContext();
|
|
34
|
+
_auth = createAuth({ d1: env.DB, schema });
|
|
35
|
+
return _auth;
|
|
36
|
+
}
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
### Next.js App Router catch-all route
|
|
40
|
+
|
|
41
|
+
```ts
|
|
42
|
+
// app/api/auth/[...all]/route.ts
|
|
43
|
+
import { toNextHandler } from '@saas-maker/auth-preset/next';
|
|
44
|
+
import { getAuth } from '@/lib/auth';
|
|
45
|
+
|
|
46
|
+
export const { GET, POST } = toNextHandler(getAuth());
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
## Client — React
|
|
50
|
+
|
|
51
|
+
```tsx
|
|
52
|
+
// app/providers.tsx
|
|
53
|
+
'use client';
|
|
54
|
+
import { AuthProvider } from '@saas-maker/auth-preset/client';
|
|
55
|
+
|
|
56
|
+
export function Providers({ children }) {
|
|
57
|
+
return <AuthProvider>{children}</AuthProvider>;
|
|
58
|
+
}
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
```tsx
|
|
62
|
+
'use client';
|
|
63
|
+
import { useSession, useAuthClient } from '@saas-maker/auth-preset/client';
|
|
64
|
+
|
|
65
|
+
export function Header() {
|
|
66
|
+
const { data: session } = useSession();
|
|
67
|
+
const client = useAuthClient();
|
|
68
|
+
if (!session) return <button onClick={() => client.signIn.social({ provider: 'google' })}>Sign in</button>;
|
|
69
|
+
return <button onClick={() => client.signOut()}>Sign out {session.user.email}</button>;
|
|
70
|
+
}
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
## Defaults baked in
|
|
74
|
+
|
|
75
|
+
| Setting | Default |
|
|
76
|
+
|---|---|
|
|
77
|
+
| Provider | `google` |
|
|
78
|
+
| Session cookie name | `foundry.session` |
|
|
79
|
+
| `secure` cookie | `true` in production, `false` otherwise |
|
|
80
|
+
| `sameSite` | `lax` |
|
|
81
|
+
| `httpOnly` | `true` |
|
|
82
|
+
| Cross-subdomain cookies | disabled |
|
|
83
|
+
| `trustedOrigins` | `[baseURL]` |
|
|
84
|
+
|
|
85
|
+
Override anything by passing it explicitly to `createAuth({ ... })`.
|
|
86
|
+
|
|
87
|
+
## Schema
|
|
88
|
+
|
|
89
|
+
The D1 adapter expects the standard better-auth tables (`user`, `session`, `account`, `verification`). Generate them with:
|
|
90
|
+
|
|
91
|
+
```bash
|
|
92
|
+
pnpm dlx better-auth-cli generate --schema ./src/lib/auth-schema.ts
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
Then run a Drizzle migration to create the tables in D1.
|