@farm.js/better-auth 0.1.50 → 0.1.51-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.
Files changed (2) hide show
  1. package/README.md +130 -3
  2. package/package.json +3 -3
package/README.md CHANGED
@@ -1,11 +1,138 @@
1
1
  # @farm.js/better-auth
2
2
 
3
- Better Auth integration for Farm.js
3
+ Better Auth route mounting for Farm.js applications. Pass an application-owned Better Auth instance
4
+ and Farm mounts its handler at `/api/auth/[...auth]`, so there is no route module to maintain. The
5
+ application keeps full control of the database adapter, plugins, providers, and callbacks, and uses
6
+ Better Auth's own server API and client.
4
7
 
5
8
  See [Stability and Support](https://farmjs.dev/docs/stability) for what this package guarantees.
6
9
 
10
+ ## Install
11
+
12
+ ```bash
13
+ pnpm add @farm.js/better-auth better-auth
14
+ ```
15
+
16
+ This package has no Better Auth dependency of its own. The application installs `better-auth` and
17
+ the database driver it uses. `farm add integration better-auth --ui` adds `better-auth` with a local
18
+ SQLite driver, writes `src/lib/auth.ts` and `.env.example`, and adds a working sign-in screen.
19
+
20
+ To start from a complete app instead:
21
+
7
22
  ```bash
8
- npm install @farm.js/better-auth
23
+ pnpm create @farm.js/app my-app --template better-auth
24
+ ```
25
+
26
+ For plain email/password with no Better Auth configuration in app code, use top-level Farm Auth,
27
+ [`@farm.js/auth`](https://www.npmjs.com/package/@farm.js/auth), instead.
28
+
29
+ ## Create the instance
30
+
31
+ This example uses a local SQLite file through `better-sqlite3` (`pnpm add better-sqlite3`):
32
+
33
+ ```ts
34
+ // src/lib/auth.ts
35
+ import Database from "better-sqlite3";
36
+ import { betterAuth } from "better-auth";
37
+ import { getMigrations } from "better-auth/db/migration";
38
+
39
+ export const auth = betterAuth({
40
+ database: new Database(process.env.BETTER_AUTH_DATABASE_PATH || "better-auth.sqlite"),
41
+ secret: process.env.BETTER_AUTH_SECRET,
42
+ baseURL: process.env.BETTER_AUTH_URL,
43
+ emailAndPassword: {
44
+ enabled: true,
45
+ },
46
+ });
47
+
48
+ const migrations = await getMigrations(auth.options);
49
+ await migrations.runMigrations();
50
+ ```
51
+
52
+ This SQLite setup is meant for local development. Configure a persistent production database and
53
+ managed migrations in Better Auth before deploying.
54
+
55
+ ## Register it
56
+
57
+ ```ts
58
+ // src/lib/integrations.ts
59
+ import { betterAuth } from "@farm.js/better-auth";
60
+ import { auth } from "./auth";
61
+
62
+ export const appIntegrations = {
63
+ auth: betterAuth({
64
+ instance: auth,
65
+ }),
66
+ } as const;
67
+ ```
68
+
69
+ ```ts
70
+ // farm.config.ts
71
+ import { defineConfig } from "@farm.js/core";
72
+ import { appIntegrations } from "./src/lib/integrations";
73
+
74
+ export default defineConfig({
75
+ integrations: appIntegrations,
76
+ });
9
77
  ```
10
78
 
11
- See the [Farm.js repository](https://github.com/farming-labs/farm.js) for documentation, examples, and support.
79
+ Farm passes every request under the catch-all to `instance.handler(request)`:
80
+
81
+ ```text
82
+ GET /api/auth/[...auth]
83
+ POST /api/auth/[...auth]
84
+ ```
85
+
86
+ That is Better Auth's default `/api/auth` base path. Do not also set the top-level `auth` key: both
87
+ own the same route, and Farm reports a configuration error rather than choosing one.
88
+
89
+ ## Use Better Auth
90
+
91
+ Use Better Auth's client in the browser. An empty `baseURL` keeps calls on the current origin:
92
+
93
+ ```ts
94
+ // src/lib/auth-client.ts
95
+ import { createAuthClient } from "better-auth/react";
96
+
97
+ export const authClient = createAuthClient({
98
+ baseURL: "",
99
+ });
100
+ ```
101
+
102
+ ```ts
103
+ await authClient.signIn.email({ email: "ada@example.com", password: "a-long-password" });
104
+ const session = await authClient.getSession();
105
+ await authClient.signOut();
106
+ ```
107
+
108
+ Methods and response types come from Better Auth and its plugins. Farm generates no parallel
109
+ `api.auth` caller for this integration.
110
+
111
+ `betterAuth(...)` has no `protectedRoutes` option. Read the session with Better Auth in server code,
112
+ or call the instance from [Farm middleware](https://farmjs.dev/docs/middleware), then apply the
113
+ app's authorization rules.
114
+
115
+ ## Options
116
+
117
+ | Option | Required | Notes |
118
+ | ---------- | -------- | -------------------------------------------------------------- |
119
+ | `instance` | Yes | A Better Auth instance, or any object with `handler(request)`. |
120
+ | `log` | No | Integration lifecycle and route logger. |
121
+
122
+ ## Production checklist
123
+
124
+ - Set a strong `BETTER_AUTH_SECRET` and the public `BETTER_AUTH_URL`.
125
+ - Use a persistent database and run Better Auth migrations before serving traffic.
126
+ - Keep database and OAuth credentials server-only.
127
+ - Verify trusted origins, cookies, and proxy headers on the deployed origin.
128
+
129
+ When Better Auth runs inside Convex as a component, this integration is not used;
130
+ [`@farm.js/convex`](https://farmjs.dev/docs/integrations/convex) handles the auth routes instead.
131
+
132
+ ## Documentation
133
+
134
+ - [Better Auth integration](https://farmjs.dev/docs/integrations/auth/better-auth)
135
+ - [Auth integrations overview](https://farmjs.dev/docs/integrations/auth)
136
+
137
+ Report issues and find examples in the
138
+ [Farm.js repository](https://github.com/farming-labs/farm.js).
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@farm.js/better-auth",
3
- "version": "0.1.50",
3
+ "version": "0.1.51-beta.1",
4
4
  "description": "Better Auth integration for Farm.js",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -25,8 +25,8 @@
25
25
  "access": "public"
26
26
  },
27
27
  "dependencies": {
28
- "@farm.js/integration-utils": "0.1.50",
29
- "@farm.js/core": "0.1.50"
28
+ "@farm.js/integration-utils": "0.1.51-beta.1",
29
+ "@farm.js/core": "0.1.51-beta.1"
30
30
  },
31
31
  "scripts": {
32
32
  "build": "node -e \"require('node:fs').rmSync('dist', { recursive: true, force: true })\" && tsc",