@molecule/api-external-auth-supabase 1.0.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/LICENSE +115 -0
- package/dist/browser-guard.d.ts +2 -0
- package/dist/browser-guard.d.ts.map +1 -0
- package/dist/browser-guard.js +18 -0
- package/dist/client.d.ts +40 -0
- package/dist/client.d.ts.map +1 -0
- package/dist/client.js +84 -0
- package/dist/config.d.ts +13 -0
- package/dist/config.d.ts.map +1 -0
- package/dist/config.js +16 -0
- package/dist/index.d.ts +86 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +85 -0
- package/dist/provider.d.ts +17 -0
- package/dist/provider.d.ts.map +1 -0
- package/dist/provider.js +36 -0
- package/dist/state.d.ts +37 -0
- package/dist/state.d.ts.map +1 -0
- package/dist/state.js +25 -0
- package/dist/types.d.ts +35 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +6 -0
- package/package.json +52 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work.
|
|
38
|
+
|
|
39
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
40
|
+
form, that is based on (or derived from) the Work and for which the
|
|
41
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
42
|
+
represent, as a whole, an original work of authorship.
|
|
43
|
+
|
|
44
|
+
"Contribution" shall mean any work of authorship, including the
|
|
45
|
+
original version of the Work and any modifications or additions
|
|
46
|
+
to that Work, that is intentionally submitted to the Licensor for
|
|
47
|
+
inclusion in the Work by the copyright owner or by an individual or
|
|
48
|
+
Legal Entity authorized to submit on behalf of the copyright owner.
|
|
49
|
+
|
|
50
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
51
|
+
on behalf of whom a Contribution has been received by the Licensor and
|
|
52
|
+
subsequently incorporated within the Work.
|
|
53
|
+
|
|
54
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
55
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
56
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
57
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
58
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
59
|
+
Work and such Derivative Works in Source or Object form.
|
|
60
|
+
|
|
61
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
62
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
63
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
64
|
+
patent license to make, have made, use, offer to sell, sell, import,
|
|
65
|
+
and otherwise transfer the Work.
|
|
66
|
+
|
|
67
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
68
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
69
|
+
modifications, and in Source or Object form, provided that You
|
|
70
|
+
meet the following conditions:
|
|
71
|
+
|
|
72
|
+
(a) You must give any other recipients of the Work or
|
|
73
|
+
Derivative Works a copy of this License; and
|
|
74
|
+
|
|
75
|
+
(b) You must cause any modified files to carry prominent notices
|
|
76
|
+
stating that You changed the files; and
|
|
77
|
+
|
|
78
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
79
|
+
that You distribute, all copyright, patent, trademark, and
|
|
80
|
+
attribution notices from the Source form of the Work,
|
|
81
|
+
excluding those notices that do not pertain to any part of
|
|
82
|
+
the Derivative Works; and
|
|
83
|
+
|
|
84
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
85
|
+
distribution, then any Derivative Works that You distribute must
|
|
86
|
+
include a readable copy of the attribution notices contained
|
|
87
|
+
within such NOTICE file.
|
|
88
|
+
|
|
89
|
+
5. Submission of Contributions.
|
|
90
|
+
|
|
91
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
92
|
+
names, trademarks, service marks, or product names of the Licensor.
|
|
93
|
+
|
|
94
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
95
|
+
agreed to in writing, Licensor provides the Work on an "AS IS" BASIS,
|
|
96
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND.
|
|
97
|
+
|
|
98
|
+
8. Limitation of Liability. In no event and under no legal theory shall
|
|
99
|
+
any Contributor be liable to You for damages.
|
|
100
|
+
|
|
101
|
+
9. Accepting Warranty or Additional Liability.
|
|
102
|
+
|
|
103
|
+
Copyright 2026 Molecule Dev, Inc.
|
|
104
|
+
|
|
105
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
106
|
+
you may not use this file except in compliance with the License.
|
|
107
|
+
You may obtain a copy of the License at
|
|
108
|
+
|
|
109
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
110
|
+
|
|
111
|
+
Unless required by applicable law or agreed to in writing, software
|
|
112
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
113
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
114
|
+
See the License for the specific language governing permissions and
|
|
115
|
+
limitations under the License.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"browser-guard.d.ts","sourceRoot":"","sources":["../src/browser-guard.ts"],"names":[],"mappings":"AAuBA,OAAO,EAAE,CAAA"}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Browser guard — `@molecule/api-external-auth-supabase` is SERVER-ONLY.
|
|
3
|
+
*
|
|
4
|
+
* Generated by scripts/gen-browser-guards.mjs (workspace root) — edit THAT, not this.
|
|
5
|
+
* Evaluating a server package in a browser bundle is always an import-graph mistake
|
|
6
|
+
* (node APIs, secrets); without this guard it surfaces as a cryptic downstream crash
|
|
7
|
+
* ("Buffer is not defined") far from the culprit. Throwing here names the package and
|
|
8
|
+
* the fix at the exact moment the client bundle evaluates it. jsdom tests and SSR are
|
|
9
|
+
* unaffected: the throw requires browser globals AND the absence of a node runtime.
|
|
10
|
+
*/
|
|
11
|
+
const g = globalThis;
|
|
12
|
+
if (g.window !== undefined && g.document !== undefined && !g.process?.versions?.node) {
|
|
13
|
+
throw new Error('@molecule/api-external-auth-supabase is SERVER-ONLY: it was bundled into browser/client code. Import it only ' +
|
|
14
|
+
'from server code (a server route/function or your API), or dynamic-import it inside ' +
|
|
15
|
+
'the server handler — never from components or shared client modules, and never ' +
|
|
16
|
+
'polyfill Buffer/process to silence this.');
|
|
17
|
+
}
|
|
18
|
+
export {};
|
package/dist/client.d.ts
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { type SupabaseClient } from '@supabase/supabase-js';
|
|
2
|
+
/**
|
|
3
|
+
* Returns the anon-key Supabase client (lazy singleton). Created on first
|
|
4
|
+
* call from the resolved settings — explicit `configureSupabase()` values
|
|
5
|
+
* first, env fallbacks second (`SUPABASE_URL` ?? `VITE_SUPABASE_URL`, and
|
|
6
|
+
* `SUPABASE_ANON_KEY` ?? `VITE_SUPABASE_ANON_KEY` ??
|
|
7
|
+
* `VITE_SUPABASE_PUBLISHABLE_KEY`).
|
|
8
|
+
*
|
|
9
|
+
* The anon key is PUBLIC — this client requires no secret and is constrained
|
|
10
|
+
* by Row Level Security. Use it for `verifyUserToken()` and for PostgREST
|
|
11
|
+
* reads/writes that RLS already permits.
|
|
12
|
+
*
|
|
13
|
+
* @throws {Error} When neither `configureSupabase()` nor the environment provides a
|
|
14
|
+
* URL + anon key — the error names exactly which env keys are missing.
|
|
15
|
+
*/
|
|
16
|
+
export declare const getAnonClient: () => SupabaseClient;
|
|
17
|
+
/**
|
|
18
|
+
* Whether a service-role key is available (explicitly configured or via
|
|
19
|
+
* `SUPABASE_SERVICE_ROLE_KEY`). ALWAYS check this before calling
|
|
20
|
+
* `getServiceClient()` — molecule sandboxes do NOT provision the key by
|
|
21
|
+
* default, so admin paths must degrade gracefully or ask the user to
|
|
22
|
+
* connect it.
|
|
23
|
+
*/
|
|
24
|
+
export declare const hasServiceRole: () => boolean;
|
|
25
|
+
/**
|
|
26
|
+
* Returns the service-role (admin) Supabase client (lazy singleton). Bypasses
|
|
27
|
+
* Row Level Security — server-only, for admin operations the anon client
|
|
28
|
+
* cannot perform.
|
|
29
|
+
*
|
|
30
|
+
* @throws {Error} When no service-role key is connected (guard with
|
|
31
|
+
* `hasServiceRole()`), or when no project URL is configured.
|
|
32
|
+
*/
|
|
33
|
+
export declare const getServiceClient: () => SupabaseClient;
|
|
34
|
+
/**
|
|
35
|
+
* Test hook: clears the cached clients AND any settings set via
|
|
36
|
+
* `configureSupabase()`, returning the module to its pristine state. The next
|
|
37
|
+
* client creation re-reads the environment from scratch.
|
|
38
|
+
*/
|
|
39
|
+
export declare const resetSupabase: () => void;
|
|
40
|
+
//# sourceMappingURL=client.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":"AAAA,OAAO,EAAgB,KAAK,cAAc,EAAE,MAAM,uBAAuB,CAAA;AAgBzE;;;;;;;;;;;;;GAaG;AACH,eAAO,MAAM,aAAa,QAAO,cAiBhC,CAAA;AAED;;;;;;GAMG;AACH,eAAO,MAAM,cAAc,QAAO,OAAoD,CAAA;AAEtF;;;;;;;GAOG;AACH,eAAO,MAAM,gBAAgB,QAAO,cAgBnC,CAAA;AAED;;;;GAIG;AACH,eAAO,MAAM,aAAa,QAAO,IAIhC,CAAA"}
|
package/dist/client.js
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import { createClient } from '@supabase/supabase-js';
|
|
2
|
+
import { resolveSettings, state } from './state.js';
|
|
3
|
+
/**
|
|
4
|
+
* Server-appropriate client options: no session persistence, no background
|
|
5
|
+
* token refresh, no URL-fragment session detection.
|
|
6
|
+
*/
|
|
7
|
+
const SERVER_CLIENT_OPTIONS = {
|
|
8
|
+
auth: {
|
|
9
|
+
persistSession: false,
|
|
10
|
+
autoRefreshToken: false,
|
|
11
|
+
detectSessionInUrl: false,
|
|
12
|
+
},
|
|
13
|
+
};
|
|
14
|
+
/**
|
|
15
|
+
* Returns the anon-key Supabase client (lazy singleton). Created on first
|
|
16
|
+
* call from the resolved settings — explicit `configureSupabase()` values
|
|
17
|
+
* first, env fallbacks second (`SUPABASE_URL` ?? `VITE_SUPABASE_URL`, and
|
|
18
|
+
* `SUPABASE_ANON_KEY` ?? `VITE_SUPABASE_ANON_KEY` ??
|
|
19
|
+
* `VITE_SUPABASE_PUBLISHABLE_KEY`).
|
|
20
|
+
*
|
|
21
|
+
* The anon key is PUBLIC — this client requires no secret and is constrained
|
|
22
|
+
* by Row Level Security. Use it for `verifyUserToken()` and for PostgREST
|
|
23
|
+
* reads/writes that RLS already permits.
|
|
24
|
+
*
|
|
25
|
+
* @throws {Error} When neither `configureSupabase()` nor the environment provides a
|
|
26
|
+
* URL + anon key — the error names exactly which env keys are missing.
|
|
27
|
+
*/
|
|
28
|
+
export const getAnonClient = () => {
|
|
29
|
+
if (state.anonClient)
|
|
30
|
+
return state.anonClient;
|
|
31
|
+
const { url, anonKey } = resolveSettings();
|
|
32
|
+
if (!url || !anonKey) {
|
|
33
|
+
const missing = [
|
|
34
|
+
...(url ? [] : ['SUPABASE_URL (or VITE_SUPABASE_URL)']),
|
|
35
|
+
...(anonKey
|
|
36
|
+
? []
|
|
37
|
+
: ['SUPABASE_ANON_KEY (or VITE_SUPABASE_ANON_KEY / VITE_SUPABASE_PUBLISHABLE_KEY)']),
|
|
38
|
+
];
|
|
39
|
+
throw new Error(`@molecule/api-supabase is not configured: missing ${missing.join(' and ')}. ` +
|
|
40
|
+
'Set the env var(s) in the Environment panel or call configureSupabase({ url, anonKey }).');
|
|
41
|
+
}
|
|
42
|
+
state.anonClient = createClient(url, anonKey, SERVER_CLIENT_OPTIONS);
|
|
43
|
+
return state.anonClient;
|
|
44
|
+
};
|
|
45
|
+
/**
|
|
46
|
+
* Whether a service-role key is available (explicitly configured or via
|
|
47
|
+
* `SUPABASE_SERVICE_ROLE_KEY`). ALWAYS check this before calling
|
|
48
|
+
* `getServiceClient()` — molecule sandboxes do NOT provision the key by
|
|
49
|
+
* default, so admin paths must degrade gracefully or ask the user to
|
|
50
|
+
* connect it.
|
|
51
|
+
*/
|
|
52
|
+
export const hasServiceRole = () => Boolean(resolveSettings().serviceRoleKey);
|
|
53
|
+
/**
|
|
54
|
+
* Returns the service-role (admin) Supabase client (lazy singleton). Bypasses
|
|
55
|
+
* Row Level Security — server-only, for admin operations the anon client
|
|
56
|
+
* cannot perform.
|
|
57
|
+
*
|
|
58
|
+
* @throws {Error} When no service-role key is connected (guard with
|
|
59
|
+
* `hasServiceRole()`), or when no project URL is configured.
|
|
60
|
+
*/
|
|
61
|
+
export const getServiceClient = () => {
|
|
62
|
+
if (state.serviceClient)
|
|
63
|
+
return state.serviceClient;
|
|
64
|
+
const { url, serviceRoleKey } = resolveSettings();
|
|
65
|
+
if (!serviceRoleKey) {
|
|
66
|
+
throw new Error('SUPABASE_SERVICE_ROLE_KEY is not connected. Ask the user to add it in the Environment panel — never assume it exists.');
|
|
67
|
+
}
|
|
68
|
+
if (!url) {
|
|
69
|
+
throw new Error('@molecule/api-supabase is not configured: missing SUPABASE_URL (or VITE_SUPABASE_URL). ' +
|
|
70
|
+
'Set the env var in the Environment panel or call configureSupabase({ url }).');
|
|
71
|
+
}
|
|
72
|
+
state.serviceClient = createClient(url, serviceRoleKey, SERVER_CLIENT_OPTIONS);
|
|
73
|
+
return state.serviceClient;
|
|
74
|
+
};
|
|
75
|
+
/**
|
|
76
|
+
* Test hook: clears the cached clients AND any settings set via
|
|
77
|
+
* `configureSupabase()`, returning the module to its pristine state. The next
|
|
78
|
+
* client creation re-reads the environment from scratch.
|
|
79
|
+
*/
|
|
80
|
+
export const resetSupabase = () => {
|
|
81
|
+
state.settings = {};
|
|
82
|
+
state.anonClient = null;
|
|
83
|
+
state.serviceClient = null;
|
|
84
|
+
};
|
package/dist/config.d.ts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { SupabaseSettings } from './types.js';
|
|
2
|
+
/**
|
|
3
|
+
* Explicitly sets (part of) the Supabase connection settings, overriding the
|
|
4
|
+
* corresponding environment-variable fallbacks.
|
|
5
|
+
*
|
|
6
|
+
* Merges with any previously configured settings, so it can be called with
|
|
7
|
+
* just the piece that changed (e.g. only `serviceRoleKey` once the user
|
|
8
|
+
* connects it). Invalidates the cached client singletons so the next
|
|
9
|
+
* `getAnonClient()` / `getServiceClient()` call reflects the new settings.
|
|
10
|
+
* Use `resetSupabase()` for a full clean slate.
|
|
11
|
+
*/
|
|
12
|
+
export declare const configureSupabase: (settings: SupabaseSettings) => void;
|
|
13
|
+
//# sourceMappingURL=config.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"config.d.ts","sourceRoot":"","sources":["../src/config.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,YAAY,CAAA;AAElD;;;;;;;;;GASG;AACH,eAAO,MAAM,iBAAiB,GAAI,UAAU,gBAAgB,KAAG,IAI9D,CAAA"}
|
package/dist/config.js
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { state } from './state.js';
|
|
2
|
+
/**
|
|
3
|
+
* Explicitly sets (part of) the Supabase connection settings, overriding the
|
|
4
|
+
* corresponding environment-variable fallbacks.
|
|
5
|
+
*
|
|
6
|
+
* Merges with any previously configured settings, so it can be called with
|
|
7
|
+
* just the piece that changed (e.g. only `serviceRoleKey` once the user
|
|
8
|
+
* connects it). Invalidates the cached client singletons so the next
|
|
9
|
+
* `getAnonClient()` / `getServiceClient()` call reflects the new settings.
|
|
10
|
+
* Use `resetSupabase()` for a full clean slate.
|
|
11
|
+
*/
|
|
12
|
+
export const configureSupabase = (settings) => {
|
|
13
|
+
state.settings = { ...state.settings, ...settings };
|
|
14
|
+
state.anonClient = null;
|
|
15
|
+
state.serviceClient = null;
|
|
16
|
+
};
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* External authentication provider using Supabase — first-class support for
|
|
3
|
+
* apps imported into molecule.dev that were built on Supabase (Lovable and
|
|
4
|
+
* similar).
|
|
5
|
+
*
|
|
6
|
+
* Implements the `@molecule/api-external-auth` contract: the exported
|
|
7
|
+
* `provider` verifies a user's Supabase JWT server-side via the anon client's
|
|
8
|
+
* `auth.getUser(token)`. Two tiers:
|
|
9
|
+
*
|
|
10
|
+
* - **No-secret tier** (works out of the box): token verification and
|
|
11
|
+
* RLS-constrained PostgREST access via `getAnonClient()` — both using only
|
|
12
|
+
* the PUBLIC anon key (`SUPABASE_ANON_KEY` / `VITE_SUPABASE_ANON_KEY` /
|
|
13
|
+
* `VITE_SUPABASE_PUBLISHABLE_KEY`). No secret required.
|
|
14
|
+
* - **Connected tier**: once the user supplies `SUPABASE_SERVICE_ROLE_KEY`,
|
|
15
|
+
* `getServiceClient()` returns an admin client that bypasses Row Level
|
|
16
|
+
* Security. Gate every admin path with `hasServiceRole()`.
|
|
17
|
+
*
|
|
18
|
+
* Settings come from `configureSupabase()` or, lazily at first client
|
|
19
|
+
* creation, from env: `SUPABASE_URL` ?? `VITE_SUPABASE_URL`,
|
|
20
|
+
* `SUPABASE_ANON_KEY` ?? `VITE_SUPABASE_ANON_KEY` ??
|
|
21
|
+
* `VITE_SUPABASE_PUBLISHABLE_KEY`, `SUPABASE_SERVICE_ROLE_KEY`.
|
|
22
|
+
* `resetSupabase()` clears cached clients + settings (test hook).
|
|
23
|
+
*
|
|
24
|
+
* @example
|
|
25
|
+
* ```ts
|
|
26
|
+
* // bonds.ts — wire the provider at startup:
|
|
27
|
+
* import { setProvider } from '@molecule/api-external-auth'
|
|
28
|
+
* import { provider } from '@molecule/api-external-auth-supabase'
|
|
29
|
+
*
|
|
30
|
+
* setProvider(provider)
|
|
31
|
+
*
|
|
32
|
+
* // Then server routes verify the token the frontend already sends:
|
|
33
|
+
* import { verifyUserToken } from '@molecule/api-external-auth'
|
|
34
|
+
*
|
|
35
|
+
* app.get('/api/me', async (req, res) => {
|
|
36
|
+
* const token = req.headers.authorization?.replace(/^Bearer /, '') ?? ''
|
|
37
|
+
* const user = await verifyUserToken(token)
|
|
38
|
+
* if (!user) {
|
|
39
|
+
* res.status(401).json({ error: 'Invalid or expired session.' })
|
|
40
|
+
* return
|
|
41
|
+
* }
|
|
42
|
+
* res.json({ userId: user.userId, email: user.email })
|
|
43
|
+
* })
|
|
44
|
+
* ```
|
|
45
|
+
*
|
|
46
|
+
* @example
|
|
47
|
+
* ```ts
|
|
48
|
+
* // Admin operations need the service-role key — ALWAYS gate on hasServiceRole():
|
|
49
|
+
* import { getServiceClient, hasServiceRole } from '@molecule/api-external-auth-supabase'
|
|
50
|
+
*
|
|
51
|
+
* if (hasServiceRole()) {
|
|
52
|
+
* const admin = getServiceClient()
|
|
53
|
+
* await admin.from('profiles').update({ suspended: true }).eq('id', targetId)
|
|
54
|
+
* } else {
|
|
55
|
+
* // Degrade: store the flag in the provisioned DATABASE_URL Postgres,
|
|
56
|
+
* // or ask the user to connect SUPABASE_SERVICE_ROLE_KEY in the
|
|
57
|
+
* // Environment panel.
|
|
58
|
+
* }
|
|
59
|
+
* ```
|
|
60
|
+
*
|
|
61
|
+
* @remarks
|
|
62
|
+
* - The anon key is PUBLIC (it ships in the browser bundle of every Supabase
|
|
63
|
+
* app) — `provider.verifyUserToken()` and `getAnonClient()` need NO secret.
|
|
64
|
+
* Do not treat a missing service-role key as "Supabase is unusable".
|
|
65
|
+
* - `SUPABASE_SERVICE_ROLE_KEY` is NOT provisioned in molecule sandboxes and
|
|
66
|
+
* never will be by default. Check `hasServiceRole()` and degrade gracefully
|
|
67
|
+
* or ask the user to connect it in the Environment panel — never assume it
|
|
68
|
+
* exists. Migrations/DDL against the user's hosted Supabase database cannot
|
|
69
|
+
* run from the sandbox.
|
|
70
|
+
* - New server-side tables belong in the provisioned `DATABASE_URL` Postgres,
|
|
71
|
+
* NOT in the user's hosted Supabase — unless the user has connected
|
|
72
|
+
* Supabase credentials that allow it.
|
|
73
|
+
* - This package is SERVER-ONLY. It throws immediately if bundled into
|
|
74
|
+
* browser/client code — import it only from server code, and never polyfill
|
|
75
|
+
* `Buffer`/`process` to silence the guard.
|
|
76
|
+
*
|
|
77
|
+
* @see https://www.npmjs.com/package/@supabase/supabase-js
|
|
78
|
+
*
|
|
79
|
+
* @module
|
|
80
|
+
*/
|
|
81
|
+
export * from './browser-guard.js';
|
|
82
|
+
export * from './client.js';
|
|
83
|
+
export * from './config.js';
|
|
84
|
+
export * from './provider.js';
|
|
85
|
+
export * from './types.js';
|
|
86
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA+EG;AAEH,cAAc,oBAAoB,CAAA;AAClC,cAAc,aAAa,CAAA;AAC3B,cAAc,aAAa,CAAA;AAC3B,cAAc,eAAe,CAAA;AAC7B,cAAc,YAAY,CAAA"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* External authentication provider using Supabase — first-class support for
|
|
3
|
+
* apps imported into molecule.dev that were built on Supabase (Lovable and
|
|
4
|
+
* similar).
|
|
5
|
+
*
|
|
6
|
+
* Implements the `@molecule/api-external-auth` contract: the exported
|
|
7
|
+
* `provider` verifies a user's Supabase JWT server-side via the anon client's
|
|
8
|
+
* `auth.getUser(token)`. Two tiers:
|
|
9
|
+
*
|
|
10
|
+
* - **No-secret tier** (works out of the box): token verification and
|
|
11
|
+
* RLS-constrained PostgREST access via `getAnonClient()` — both using only
|
|
12
|
+
* the PUBLIC anon key (`SUPABASE_ANON_KEY` / `VITE_SUPABASE_ANON_KEY` /
|
|
13
|
+
* `VITE_SUPABASE_PUBLISHABLE_KEY`). No secret required.
|
|
14
|
+
* - **Connected tier**: once the user supplies `SUPABASE_SERVICE_ROLE_KEY`,
|
|
15
|
+
* `getServiceClient()` returns an admin client that bypasses Row Level
|
|
16
|
+
* Security. Gate every admin path with `hasServiceRole()`.
|
|
17
|
+
*
|
|
18
|
+
* Settings come from `configureSupabase()` or, lazily at first client
|
|
19
|
+
* creation, from env: `SUPABASE_URL` ?? `VITE_SUPABASE_URL`,
|
|
20
|
+
* `SUPABASE_ANON_KEY` ?? `VITE_SUPABASE_ANON_KEY` ??
|
|
21
|
+
* `VITE_SUPABASE_PUBLISHABLE_KEY`, `SUPABASE_SERVICE_ROLE_KEY`.
|
|
22
|
+
* `resetSupabase()` clears cached clients + settings (test hook).
|
|
23
|
+
*
|
|
24
|
+
* @example
|
|
25
|
+
* ```ts
|
|
26
|
+
* // bonds.ts — wire the provider at startup:
|
|
27
|
+
* import { setProvider } from '@molecule/api-external-auth'
|
|
28
|
+
* import { provider } from '@molecule/api-external-auth-supabase'
|
|
29
|
+
*
|
|
30
|
+
* setProvider(provider)
|
|
31
|
+
*
|
|
32
|
+
* // Then server routes verify the token the frontend already sends:
|
|
33
|
+
* import { verifyUserToken } from '@molecule/api-external-auth'
|
|
34
|
+
*
|
|
35
|
+
* app.get('/api/me', async (req, res) => {
|
|
36
|
+
* const token = req.headers.authorization?.replace(/^Bearer /, '') ?? ''
|
|
37
|
+
* const user = await verifyUserToken(token)
|
|
38
|
+
* if (!user) {
|
|
39
|
+
* res.status(401).json({ error: 'Invalid or expired session.' })
|
|
40
|
+
* return
|
|
41
|
+
* }
|
|
42
|
+
* res.json({ userId: user.userId, email: user.email })
|
|
43
|
+
* })
|
|
44
|
+
* ```
|
|
45
|
+
*
|
|
46
|
+
* @example
|
|
47
|
+
* ```ts
|
|
48
|
+
* // Admin operations need the service-role key — ALWAYS gate on hasServiceRole():
|
|
49
|
+
* import { getServiceClient, hasServiceRole } from '@molecule/api-external-auth-supabase'
|
|
50
|
+
*
|
|
51
|
+
* if (hasServiceRole()) {
|
|
52
|
+
* const admin = getServiceClient()
|
|
53
|
+
* await admin.from('profiles').update({ suspended: true }).eq('id', targetId)
|
|
54
|
+
* } else {
|
|
55
|
+
* // Degrade: store the flag in the provisioned DATABASE_URL Postgres,
|
|
56
|
+
* // or ask the user to connect SUPABASE_SERVICE_ROLE_KEY in the
|
|
57
|
+
* // Environment panel.
|
|
58
|
+
* }
|
|
59
|
+
* ```
|
|
60
|
+
*
|
|
61
|
+
* @remarks
|
|
62
|
+
* - The anon key is PUBLIC (it ships in the browser bundle of every Supabase
|
|
63
|
+
* app) — `provider.verifyUserToken()` and `getAnonClient()` need NO secret.
|
|
64
|
+
* Do not treat a missing service-role key as "Supabase is unusable".
|
|
65
|
+
* - `SUPABASE_SERVICE_ROLE_KEY` is NOT provisioned in molecule sandboxes and
|
|
66
|
+
* never will be by default. Check `hasServiceRole()` and degrade gracefully
|
|
67
|
+
* or ask the user to connect it in the Environment panel — never assume it
|
|
68
|
+
* exists. Migrations/DDL against the user's hosted Supabase database cannot
|
|
69
|
+
* run from the sandbox.
|
|
70
|
+
* - New server-side tables belong in the provisioned `DATABASE_URL` Postgres,
|
|
71
|
+
* NOT in the user's hosted Supabase — unless the user has connected
|
|
72
|
+
* Supabase credentials that allow it.
|
|
73
|
+
* - This package is SERVER-ONLY. It throws immediately if bundled into
|
|
74
|
+
* browser/client code — import it only from server code, and never polyfill
|
|
75
|
+
* `Buffer`/`process` to silence the guard.
|
|
76
|
+
*
|
|
77
|
+
* @see https://www.npmjs.com/package/@supabase/supabase-js
|
|
78
|
+
*
|
|
79
|
+
* @module
|
|
80
|
+
*/
|
|
81
|
+
export * from './browser-guard.js';
|
|
82
|
+
export * from './client.js';
|
|
83
|
+
export * from './config.js';
|
|
84
|
+
export * from './provider.js';
|
|
85
|
+
export * from './types.js';
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* External authentication provider implementation using Supabase.
|
|
3
|
+
*
|
|
4
|
+
* Verifies Supabase user JWTs via the anon client's `auth.getUser(token)` —
|
|
5
|
+
* the anon key is PUBLIC, so verification requires NO secret.
|
|
6
|
+
*
|
|
7
|
+
* @see https://www.npmjs.com/package/@supabase/supabase-js
|
|
8
|
+
*
|
|
9
|
+
* @module
|
|
10
|
+
*/
|
|
11
|
+
import type { ExternalAuthProvider } from '@molecule/api-external-auth';
|
|
12
|
+
/**
|
|
13
|
+
* External authentication provider backed by Supabase. Wire it at startup:
|
|
14
|
+
* `setProvider(provider)` from `@molecule/api-external-auth`.
|
|
15
|
+
*/
|
|
16
|
+
export declare const provider: ExternalAuthProvider;
|
|
17
|
+
//# sourceMappingURL=provider.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"provider.d.ts","sourceRoot":"","sources":["../src/provider.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAEH,OAAO,KAAK,EAAE,oBAAoB,EAAoB,MAAM,6BAA6B,CAAA;AAIzF;;;GAGG;AACH,eAAO,MAAM,QAAQ,EAAE,oBAiBtB,CAAA"}
|
package/dist/provider.js
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* External authentication provider implementation using Supabase.
|
|
3
|
+
*
|
|
4
|
+
* Verifies Supabase user JWTs via the anon client's `auth.getUser(token)` —
|
|
5
|
+
* the anon key is PUBLIC, so verification requires NO secret.
|
|
6
|
+
*
|
|
7
|
+
* @see https://www.npmjs.com/package/@supabase/supabase-js
|
|
8
|
+
*
|
|
9
|
+
* @module
|
|
10
|
+
*/
|
|
11
|
+
import { getAnonClient } from './client.js';
|
|
12
|
+
/**
|
|
13
|
+
* External authentication provider backed by Supabase. Wire it at startup:
|
|
14
|
+
* `setProvider(provider)` from `@molecule/api-external-auth`.
|
|
15
|
+
*/
|
|
16
|
+
export const provider = {
|
|
17
|
+
async verifyUserToken(token) {
|
|
18
|
+
if (!token)
|
|
19
|
+
return null;
|
|
20
|
+
const client = getAnonClient();
|
|
21
|
+
try {
|
|
22
|
+
const { data, error } = await client.auth.getUser(token);
|
|
23
|
+
if (error || !data.user)
|
|
24
|
+
return null;
|
|
25
|
+
return { userId: data.user.id, email: data.user.email ?? undefined };
|
|
26
|
+
}
|
|
27
|
+
catch (_error) {
|
|
28
|
+
// Intentionally ignored: supabase-js reports auth/transport failures via
|
|
29
|
+
// the returned `error` (handled above); a throw here is a defensive
|
|
30
|
+
// catch for unexpected library failures, and the contract is "a token
|
|
31
|
+
// that cannot be verified is unverified (null)" — callers map null to
|
|
32
|
+
// 401, they do not need a stack trace for a bad token.
|
|
33
|
+
return null;
|
|
34
|
+
}
|
|
35
|
+
},
|
|
36
|
+
};
|
package/dist/state.d.ts
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* INTERNAL shared state for the Supabase integration — deliberately NOT
|
|
3
|
+
* re-exported from the package barrel (`src/index.ts`), so none of this leaks
|
|
4
|
+
* into the public API. Holds the configured settings, the lazily created
|
|
5
|
+
* client singletons, and the env-fallback resolution.
|
|
6
|
+
*/
|
|
7
|
+
import type { SupabaseClient } from '@supabase/supabase-js';
|
|
8
|
+
import type { SupabaseSettings } from './types.js';
|
|
9
|
+
/**
|
|
10
|
+
* Mutable module state: explicit settings plus the cached client singletons.
|
|
11
|
+
*/
|
|
12
|
+
export interface SupabaseState {
|
|
13
|
+
/**
|
|
14
|
+
* Settings supplied via `configureSupabase()`. Take precedence over env.
|
|
15
|
+
*/
|
|
16
|
+
settings: SupabaseSettings;
|
|
17
|
+
/**
|
|
18
|
+
* Cached anon-key client (lazy singleton).
|
|
19
|
+
*/
|
|
20
|
+
anonClient: SupabaseClient | null;
|
|
21
|
+
/**
|
|
22
|
+
* Cached service-role client (lazy singleton).
|
|
23
|
+
*/
|
|
24
|
+
serviceClient: SupabaseClient | null;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* The single shared state instance.
|
|
28
|
+
*/
|
|
29
|
+
export declare const state: SupabaseState;
|
|
30
|
+
/**
|
|
31
|
+
* Resolves the effective settings: explicit `configureSupabase()` values
|
|
32
|
+
* first, then environment variables. Called lazily at first client creation
|
|
33
|
+
* (and by `hasServiceRole()`), never at module load — so env vars set after
|
|
34
|
+
* import are picked up.
|
|
35
|
+
*/
|
|
36
|
+
export declare const resolveSettings: () => SupabaseSettings;
|
|
37
|
+
//# sourceMappingURL=state.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"state.d.ts","sourceRoot":"","sources":["../src/state.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,uBAAuB,CAAA;AAE3D,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,YAAY,CAAA;AAElD;;GAEG;AACH,MAAM,WAAW,aAAa;IAC5B;;OAEG;IACH,QAAQ,EAAE,gBAAgB,CAAA;IAE1B;;OAEG;IACH,UAAU,EAAE,cAAc,GAAG,IAAI,CAAA;IAEjC;;OAEG;IACH,aAAa,EAAE,cAAc,GAAG,IAAI,CAAA;CACrC;AAED;;GAEG;AACH,eAAO,MAAM,KAAK,EAAE,aAInB,CAAA;AAED;;;;;GAKG;AACH,eAAO,MAAM,eAAe,QAAO,gBAWlC,CAAA"}
|
package/dist/state.js
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The single shared state instance.
|
|
3
|
+
*/
|
|
4
|
+
export const state = {
|
|
5
|
+
settings: {},
|
|
6
|
+
anonClient: null,
|
|
7
|
+
serviceClient: null,
|
|
8
|
+
};
|
|
9
|
+
/**
|
|
10
|
+
* Resolves the effective settings: explicit `configureSupabase()` values
|
|
11
|
+
* first, then environment variables. Called lazily at first client creation
|
|
12
|
+
* (and by `hasServiceRole()`), never at module load — so env vars set after
|
|
13
|
+
* import are picked up.
|
|
14
|
+
*/
|
|
15
|
+
export const resolveSettings = () => {
|
|
16
|
+
const env = process.env;
|
|
17
|
+
return {
|
|
18
|
+
url: state.settings.url ?? env.SUPABASE_URL ?? env.VITE_SUPABASE_URL,
|
|
19
|
+
anonKey: state.settings.anonKey ??
|
|
20
|
+
env.SUPABASE_ANON_KEY ??
|
|
21
|
+
env.VITE_SUPABASE_ANON_KEY ??
|
|
22
|
+
env.VITE_SUPABASE_PUBLISHABLE_KEY,
|
|
23
|
+
serviceRoleKey: state.settings.serviceRoleKey ?? env.SUPABASE_SERVICE_ROLE_KEY,
|
|
24
|
+
};
|
|
25
|
+
};
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Type definitions for the Supabase external-auth bond.
|
|
3
|
+
*
|
|
4
|
+
* @module
|
|
5
|
+
*/
|
|
6
|
+
/**
|
|
7
|
+
* Connection settings for the Supabase integration.
|
|
8
|
+
*
|
|
9
|
+
* Every field is optional — anything not set explicitly via
|
|
10
|
+
* {@link configureSupabase} falls back to environment variables, read lazily
|
|
11
|
+
* at first client creation (never at module load):
|
|
12
|
+
*
|
|
13
|
+
* - `url`: `SUPABASE_URL` ?? `VITE_SUPABASE_URL`
|
|
14
|
+
* - `anonKey`: `SUPABASE_ANON_KEY` ?? `VITE_SUPABASE_ANON_KEY` ??
|
|
15
|
+
* `VITE_SUPABASE_PUBLISHABLE_KEY`
|
|
16
|
+
* - `serviceRoleKey`: `SUPABASE_SERVICE_ROLE_KEY`
|
|
17
|
+
*/
|
|
18
|
+
export interface SupabaseSettings {
|
|
19
|
+
/**
|
|
20
|
+
* The Supabase project URL (e.g. `https://abcd1234.supabase.co`).
|
|
21
|
+
*/
|
|
22
|
+
url?: string;
|
|
23
|
+
/**
|
|
24
|
+
* The PUBLIC anon (publishable) key. Safe to expose in browsers — it grants
|
|
25
|
+
* only what Row Level Security allows. Sufficient for verifying user JWTs.
|
|
26
|
+
*/
|
|
27
|
+
anonKey?: string;
|
|
28
|
+
/**
|
|
29
|
+
* The SECRET service-role key. Bypasses Row Level Security — server-only,
|
|
30
|
+
* never exposed to clients. Not provisioned in molecule sandboxes by
|
|
31
|
+
* default; check {@link hasServiceRole} before relying on it.
|
|
32
|
+
*/
|
|
33
|
+
serviceRoleKey?: string;
|
|
34
|
+
}
|
|
35
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH;;;;;;;;;;;GAWG;AACH,MAAM,WAAW,gBAAgB;IAC/B;;OAEG;IACH,GAAG,CAAC,EAAE,MAAM,CAAA;IAEZ;;;OAGG;IACH,OAAO,CAAC,EAAE,MAAM,CAAA;IAEhB;;;;OAIG;IACH,cAAc,CAAC,EAAE,MAAM,CAAA;CACxB"}
|
package/dist/types.js
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@molecule/api-external-auth-supabase",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "External authentication provider using Supabase for molecule.dev — verifies Supabase user JWTs server-side with the public anon key",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "dist/index.js",
|
|
7
|
+
"types": "dist/index.d.ts",
|
|
8
|
+
"scripts": {
|
|
9
|
+
"build": "tsc",
|
|
10
|
+
"test": "vitest run",
|
|
11
|
+
"test:watch": "vitest"
|
|
12
|
+
},
|
|
13
|
+
"exports": {
|
|
14
|
+
".": {
|
|
15
|
+
"types": "./dist/index.d.ts",
|
|
16
|
+
"import": "./dist/index.js"
|
|
17
|
+
}
|
|
18
|
+
},
|
|
19
|
+
"files": [
|
|
20
|
+
"dist"
|
|
21
|
+
],
|
|
22
|
+
"keywords": [
|
|
23
|
+
"molecule",
|
|
24
|
+
"external-auth",
|
|
25
|
+
"supabase",
|
|
26
|
+
"auth",
|
|
27
|
+
"provider"
|
|
28
|
+
],
|
|
29
|
+
"license": "Apache-2.0",
|
|
30
|
+
"dependencies": {
|
|
31
|
+
"@supabase/supabase-js": "2.111.0"
|
|
32
|
+
},
|
|
33
|
+
"peerDependencies": {
|
|
34
|
+
"@molecule/api-external-auth": "^1.0.0"
|
|
35
|
+
},
|
|
36
|
+
"devDependencies": {
|
|
37
|
+
"@molecule/api-external-auth": "1.0.0",
|
|
38
|
+
"@types/node": "26.1.2",
|
|
39
|
+
"typescript": "6.0.3",
|
|
40
|
+
"vitest": "4.1.10"
|
|
41
|
+
},
|
|
42
|
+
"repository": {
|
|
43
|
+
"type": "git",
|
|
44
|
+
"url": "https://github.com/molecule-dev/molecule.git",
|
|
45
|
+
"directory": "packages/api/bonds/external-auth/supabase"
|
|
46
|
+
},
|
|
47
|
+
"homepage": "https://github.com/molecule-dev/molecule/tree/main/packages/api/bonds/external-auth/supabase",
|
|
48
|
+
"bugs": "https://github.com/molecule-dev/molecule/issues",
|
|
49
|
+
"publishConfig": {
|
|
50
|
+
"access": "public"
|
|
51
|
+
}
|
|
52
|
+
}
|