@farm.js/supabase 0.1.50 → 0.1.51-beta.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 +157 -3
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -1,11 +1,165 @@
|
|
|
1
1
|
# @farm.js/supabase
|
|
2
2
|
|
|
3
|
-
Supabase
|
|
3
|
+
Supabase Auth for Farm.js applications. The integration mounts server-side routes for
|
|
4
|
+
email/password sign-in and sign-up, OAuth redirects, the callback exchange, logout, and session
|
|
5
|
+
reads, keeps Supabase's SSR cookies in sync, and can redirect signed-out visitors away from
|
|
6
|
+
protected pages.
|
|
4
7
|
|
|
5
8
|
See [Stability and Support](https://farmjs.dev/docs/stability) for what this package guarantees.
|
|
6
9
|
|
|
10
|
+
## Install
|
|
11
|
+
|
|
7
12
|
```bash
|
|
8
|
-
|
|
13
|
+
pnpm add @farm.js/supabase
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
`@supabase/ssr` and `@supabase/supabase-js` are dependencies and install with the package. To
|
|
17
|
+
scaffold the registry entry and sign-in screens in an existing app, run
|
|
18
|
+
`farm add integration supabase --ui`.
|
|
19
|
+
|
|
20
|
+
## Configure
|
|
21
|
+
|
|
22
|
+
```ts
|
|
23
|
+
// src/lib/integrations.ts
|
|
24
|
+
import { supabase } from "@farm.js/supabase";
|
|
25
|
+
|
|
26
|
+
export const appIntegrations = {
|
|
27
|
+
auth: supabase({
|
|
28
|
+
callbackPath: "/auth/callback",
|
|
29
|
+
providers: ["github"],
|
|
30
|
+
protectedRoutes: ["/dashboard(.*)"],
|
|
31
|
+
pages: {
|
|
32
|
+
signIn: "/sign-in",
|
|
33
|
+
signUp: "/sign-up",
|
|
34
|
+
},
|
|
35
|
+
}),
|
|
36
|
+
} as const;
|
|
37
|
+
|
|
38
|
+
export type AppIntegrations = typeof appIntegrations;
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
```ts
|
|
42
|
+
// farm.config.ts
|
|
43
|
+
import { defineConfig } from "@farm.js/core";
|
|
44
|
+
import { appIntegrations } from "./src/lib/integrations";
|
|
45
|
+
|
|
46
|
+
export default defineConfig({
|
|
47
|
+
integrations: appIntegrations,
|
|
48
|
+
});
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
Omit `pages` to use Farm's built-in server-rendered sign-in and sign-up forms. With custom pages,
|
|
52
|
+
`GET /auth/login` and `GET /auth/signup` redirect to them unless an OAuth provider is being started.
|
|
53
|
+
|
|
54
|
+
The integration uses the project's anonymous or publishable key. It never needs the service-role
|
|
55
|
+
key, so keep that out of `supabase(...)`. Values come from the environment:
|
|
56
|
+
|
|
57
|
+
- Project URL: `SUPABASE_URL`, then `NEXT_PUBLIC_SUPABASE_URL`.
|
|
58
|
+
- Anon or publishable key: the first one set of `SUPABASE_ANON_KEY`,
|
|
59
|
+
`NEXT_PUBLIC_SUPABASE_ANON_KEY`, `SUPABASE_PUBLISHABLE_KEY`, `NEXT_PUBLIC_SUPABASE_PUBLISHABLE_KEY`,
|
|
60
|
+
and `NEXT_PUBLIC_SUPABASE_PUBLISHABLE_DEFAULT_KEY`.
|
|
61
|
+
- Public app origin: `APP_BASE_URL`.
|
|
62
|
+
|
|
63
|
+
The URL and key are required unless an `instance` factory is passed. They can also be passed as
|
|
64
|
+
`url` and `anonKey`.
|
|
65
|
+
|
|
66
|
+
## Routes
|
|
67
|
+
|
|
68
|
+
| Method | Default route | Purpose |
|
|
69
|
+
| ------------- | ---------------- | ----------------------------------------------------------------- |
|
|
70
|
+
| `GET` | `/auth/login` | Renders or redirects to sign-in, or starts OAuth with `provider`. |
|
|
71
|
+
| `POST` | `/auth/login` | Signs in with email and password. |
|
|
72
|
+
| `GET` | `/auth/signup` | Renders or redirects to sign-up. |
|
|
73
|
+
| `POST` | `/auth/signup` | Creates an email/password account. |
|
|
74
|
+
| `GET` | `/auth/callback` | Exchanges the authorization code for a session. |
|
|
75
|
+
| `GET`, `POST` | `/auth/logout` | Signs out and redirects. |
|
|
76
|
+
| `GET` | `/auth/session` | Returns `{ authenticated, user }`, or `401` when signed out. |
|
|
77
|
+
|
|
78
|
+
Change them with `loginPath`, `signupPath`, `callbackPath`, `logoutPath`, and `sessionPath`. Every
|
|
79
|
+
route forwards Supabase's `Set-Cookie` headers, so token refreshes reach the browser.
|
|
80
|
+
|
|
81
|
+
`protectedRoutes` verifies the session with Supabase Auth (`getUser()`, not just the cookie) and
|
|
82
|
+
redirects a signed-out request to the sign-in page with the original path in `returnTo`.
|
|
83
|
+
|
|
84
|
+
## Typed calls
|
|
85
|
+
|
|
86
|
+
```ts
|
|
87
|
+
// src/lib/api.ts
|
|
88
|
+
import { createIntegrations } from "@farm.js/core/client";
|
|
89
|
+
import type { AppIntegrations } from "./integrations";
|
|
90
|
+
|
|
91
|
+
export const { api, apiClient } = createIntegrations<AppIntegrations>();
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
```ts
|
|
95
|
+
const result = await apiClient.auth.login.post({
|
|
96
|
+
body: { email: "ada@example.com", password: "a-long-password", returnTo: "/dashboard" },
|
|
97
|
+
});
|
|
98
|
+
if (result.data) window.location.assign(result.data.redirectTo);
|
|
99
|
+
|
|
100
|
+
const oauth = await apiClient.auth.oauth.get({ query: { provider: "github" } });
|
|
101
|
+
const session = await api.auth.session.get();
|
|
102
|
+
await apiClient.auth.logout.post({ body: { returnTo: "/" } });
|
|
9
103
|
```
|
|
10
104
|
|
|
11
|
-
|
|
105
|
+
`apiClient.auth.signup.post` takes the same body. When the project requires email confirmation, its
|
|
106
|
+
result includes `emailConfirmationRequired`, `email`, and a user-facing `message`.
|
|
107
|
+
|
|
108
|
+
The login and sign-up routes also accept HTML form posts with the field names in
|
|
109
|
+
`supabaseAuthFormFields` (`email`, `password`, `returnTo`). `@farm.js/supabase/client` exports that
|
|
110
|
+
object, `supabaseClient` with the operations bound to the default paths, and the request and result
|
|
111
|
+
types.
|
|
112
|
+
|
|
113
|
+
## Bring your own client
|
|
114
|
+
|
|
115
|
+
Supabase SSR clients are bound to one request's cookies, so `instance` is a factory that Farm calls
|
|
116
|
+
for every request. Keep the `options` it receives, because they carry Farm's cookie adapter:
|
|
117
|
+
|
|
118
|
+
```ts
|
|
119
|
+
import { supabase } from "@farm.js/supabase";
|
|
120
|
+
|
|
121
|
+
export const auth = supabase({
|
|
122
|
+
instance: ({ createClient, options }) =>
|
|
123
|
+
createClient(process.env.SUPABASE_URL!, process.env.SUPABASE_ANON_KEY!, {
|
|
124
|
+
...options,
|
|
125
|
+
global: {
|
|
126
|
+
...options.global,
|
|
127
|
+
headers: { ...options.global?.headers, "x-application-name": "farm-dashboard" },
|
|
128
|
+
},
|
|
129
|
+
}),
|
|
130
|
+
protectedRoutes: ["/dashboard(.*)"],
|
|
131
|
+
});
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
The factory also receives `request`, plus `url` and `anonKey` when those are configured. Do not
|
|
135
|
+
create the client outside the factory or cache what it returns.
|
|
136
|
+
|
|
137
|
+
## Options
|
|
138
|
+
|
|
139
|
+
| Option | Default | Notes |
|
|
140
|
+
| ----------------- | -------------------- | ---------------------------------------------- |
|
|
141
|
+
| `instance` | | Request-scoped client factory. |
|
|
142
|
+
| `url` | Supabase URL env | |
|
|
143
|
+
| `anonKey` | anon/publishable env | Never the service-role key. |
|
|
144
|
+
| `appBaseUrl` | `APP_BASE_URL` | Public origin for callbacks and redirects. |
|
|
145
|
+
| `callbackUrl` | | Absolute callback URL. |
|
|
146
|
+
| `callbackPath` | `/auth/callback` | Used when `callbackUrl` is not set. |
|
|
147
|
+
| `providers` | `[]` | OAuth buttons shown on the built-in form. |
|
|
148
|
+
| `defaultProvider` | | Provider started by a plain `GET /auth/login`. |
|
|
149
|
+
| `pages.signIn` | built-in form | App-owned sign-in page. |
|
|
150
|
+
| `pages.signUp` | built-in form | App-owned sign-up page. |
|
|
151
|
+
| `protectedRoutes` | | One matcher or a list. |
|
|
152
|
+
| `allowedOrigins` | app origin | Extra origins allowed to post the auth routes. |
|
|
153
|
+
| `log` | | Integration lifecycle logger. |
|
|
154
|
+
|
|
155
|
+
Sign-in, sign-up, and sign-out only accept requests from the app's own origin, plus any
|
|
156
|
+
`allowedOrigins`. A cross-site post gets `403` before any credential is read. A plain
|
|
157
|
+
`GET /auth/logout` link keeps working.
|
|
158
|
+
|
|
159
|
+
## Documentation
|
|
160
|
+
|
|
161
|
+
- [Supabase integration](https://farmjs.dev/docs/integrations/auth/supabase)
|
|
162
|
+
- [Auth integrations overview](https://farmjs.dev/docs/integrations/auth)
|
|
163
|
+
|
|
164
|
+
Report issues and find examples in the
|
|
165
|
+
[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/supabase",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.51-beta.0",
|
|
4
4
|
"description": "Supabase integration for Farm.js",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
@@ -31,8 +31,8 @@
|
|
|
31
31
|
"dependencies": {
|
|
32
32
|
"@supabase/ssr": "^0.9.0",
|
|
33
33
|
"@supabase/supabase-js": "^2.99.2",
|
|
34
|
-
"@farm.js/core": "0.1.
|
|
35
|
-
"@farm.js/integration-utils": "0.1.
|
|
34
|
+
"@farm.js/core": "0.1.51-beta.0",
|
|
35
|
+
"@farm.js/integration-utils": "0.1.51-beta.0"
|
|
36
36
|
},
|
|
37
37
|
"scripts": {
|
|
38
38
|
"build": "node -e \"require('node:fs').rmSync('dist', { recursive: true, force: true })\" && tsc",
|