@farm.js/authjs 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.
- package/README.md +112 -3
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -1,11 +1,120 @@
|
|
|
1
1
|
# @farm.js/authjs
|
|
2
2
|
|
|
3
|
-
Auth.js
|
|
3
|
+
Auth.js route mounting for Farm.js applications. Pass an application-owned Auth.js object and Farm
|
|
4
|
+
mounts its `GET` and `POST` handlers at `/api/auth/[...nextauth]`. Auth.js keeps owning providers,
|
|
5
|
+
callbacks, cookies, and sessions; Farm does not reimplement any of it.
|
|
4
6
|
|
|
5
7
|
See [Stability and Support](https://farmjs.dev/docs/stability) for what this package guarantees.
|
|
6
8
|
|
|
9
|
+
## Install
|
|
10
|
+
|
|
7
11
|
```bash
|
|
8
|
-
|
|
12
|
+
pnpm add @farm.js/authjs @auth/core
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
This package has no Auth.js dependency of its own. The application installs Auth.js and the
|
|
16
|
+
provider packages it uses. `farm add integration authjs --ui` adds `@auth/core`, writes a GitHub
|
|
17
|
+
OAuth instance to `src/lib/auth.ts`, and registers it.
|
|
18
|
+
|
|
19
|
+
## Create the Auth.js instance
|
|
20
|
+
|
|
21
|
+
The adapter needs one thing from the instance: a `handlers` object with `GET` and `POST` functions
|
|
22
|
+
that take a `Request` and return a `Response`. With `@auth/core`:
|
|
23
|
+
|
|
24
|
+
```ts
|
|
25
|
+
// src/lib/auth.ts
|
|
26
|
+
import { Auth } from "@auth/core";
|
|
27
|
+
import GitHub from "@auth/core/providers/github";
|
|
28
|
+
|
|
29
|
+
const config = {
|
|
30
|
+
// Auth.js parses actions relative to this path, so it matches the route Farm mounts.
|
|
31
|
+
basePath: "/api/auth",
|
|
32
|
+
providers: [
|
|
33
|
+
GitHub({
|
|
34
|
+
clientId: process.env.AUTH_GITHUB_ID!,
|
|
35
|
+
clientSecret: process.env.AUTH_GITHUB_SECRET!,
|
|
36
|
+
}),
|
|
37
|
+
],
|
|
38
|
+
secret: process.env.AUTH_SECRET,
|
|
39
|
+
trustHost: true,
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
const handler = (request: Request) => Auth(request, config);
|
|
43
|
+
|
|
44
|
+
export const auth = {
|
|
45
|
+
handlers: {
|
|
46
|
+
GET: handler,
|
|
47
|
+
POST: handler,
|
|
48
|
+
},
|
|
49
|
+
};
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
The object returned by `NextAuth(...)` also has this shape and can be passed directly.
|
|
53
|
+
|
|
54
|
+
## Register it
|
|
55
|
+
|
|
56
|
+
```ts
|
|
57
|
+
import { defineConfig } from "@farm.js/core";
|
|
58
|
+
import { authjs } from "@farm.js/authjs";
|
|
59
|
+
import { auth } from "./src/lib/auth";
|
|
60
|
+
|
|
61
|
+
export default defineConfig({
|
|
62
|
+
integrations: {
|
|
63
|
+
auth: authjs({
|
|
64
|
+
instance: auth,
|
|
65
|
+
}),
|
|
66
|
+
},
|
|
67
|
+
});
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
Farm mounts one catch-all route:
|
|
71
|
+
|
|
72
|
+
```text
|
|
73
|
+
GET /api/auth/[...nextauth]
|
|
74
|
+
POST /api/auth/[...nextauth]
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
Requests such as `/api/auth/session`, `/api/auth/signin`, and `/api/auth/callback/<provider>` reach
|
|
78
|
+
that route and are delegated to Auth.js. Register provider callback URLs under
|
|
79
|
+
`/api/auth/callback/<provider>`.
|
|
80
|
+
|
|
81
|
+
| Request | Behavior |
|
|
82
|
+
| ------------------------------- | -------------------------------------- |
|
|
83
|
+
| `GET` under the catch-all path | Delegated to `instance.handlers.GET`. |
|
|
84
|
+
| `POST` under the catch-all path | Delegated to `instance.handlers.POST`. |
|
|
85
|
+
| Method with no matching handler | `405 Method Not Allowed` with `Allow`. |
|
|
86
|
+
|
|
87
|
+
## Sessions and protection
|
|
88
|
+
|
|
89
|
+
There is no generated `api.auth` caller for this integration and no `protectedRoutes` option. Read
|
|
90
|
+
the session through Auth.js. In the browser, its own endpoint works:
|
|
91
|
+
|
|
92
|
+
```ts
|
|
93
|
+
const response = await fetch("/api/auth/session", { credentials: "include" });
|
|
94
|
+
const session = await response.json();
|
|
9
95
|
```
|
|
10
96
|
|
|
11
|
-
|
|
97
|
+
Protect pages with your Auth.js setup or with [Farm middleware](https://farmjs.dev/docs/middleware)
|
|
98
|
+
that checks the session, then apply the app's own authorization rules to each resource.
|
|
99
|
+
|
|
100
|
+
## Options
|
|
101
|
+
|
|
102
|
+
| Option | Required | Notes |
|
|
103
|
+
| ---------- | -------- | ----------------------------------------------- |
|
|
104
|
+
| `instance` | Yes | Object with `handlers.GET` and `handlers.POST`. |
|
|
105
|
+
| `log` | No | Integration lifecycle and route logger. |
|
|
106
|
+
|
|
107
|
+
Provider credentials, adapters, callbacks, events, and session settings all belong in the Auth.js
|
|
108
|
+
configuration. `AUTH_SECRET` and every provider credential must be set in production.
|
|
109
|
+
|
|
110
|
+
Use only one auth owner per app: either this integration or top-level Farm Auth (`auth: true`),
|
|
111
|
+
which ships as [`@farm.js/auth`](https://www.npmjs.com/package/@farm.js/auth) and needs no
|
|
112
|
+
provider setup.
|
|
113
|
+
|
|
114
|
+
## Documentation
|
|
115
|
+
|
|
116
|
+
- [Auth.js integration](https://farmjs.dev/docs/integrations/auth/authjs)
|
|
117
|
+
- [Auth integrations overview](https://farmjs.dev/docs/integrations/auth)
|
|
118
|
+
|
|
119
|
+
Report issues and find examples in the
|
|
120
|
+
[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/authjs",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.51-beta.1",
|
|
4
4
|
"description": "Auth.js 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/
|
|
29
|
-
"@farm.js/
|
|
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",
|