@supabase/server 0.1.1-rc.25 → 0.1.1-rc.26
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/dist/core/index.d.cts +194 -2
- package/dist/core/index.d.mts +194 -2
- package/dist/errors-5ivL23qo.d.mts +78 -0
- package/dist/errors-BmSsOAvx.d.cts +78 -0
- package/dist/index.cjs +0 -6
- package/dist/index.d.cts +2 -2
- package/dist/index.d.mts +2 -2
- package/dist/index.mjs +2 -2
- package/package.json +1 -1
- package/dist/create-admin-client-Cp7FxI6O.d.cts +0 -271
- package/dist/create-admin-client-ZTnl1zMe.d.mts +0 -271
package/dist/core/index.d.cts
CHANGED
|
@@ -1,3 +1,195 @@
|
|
|
1
|
-
import "../types-CnKoFCMX.cjs";
|
|
2
|
-
import {
|
|
1
|
+
import { i as Credentials, n as AllowWithKey, r as AuthResult, s as SupabaseEnv } from "../types-CnKoFCMX.cjs";
|
|
2
|
+
import { n as EnvError, t as AuthError } from "../errors-BmSsOAvx.cjs";
|
|
3
|
+
import { SupabaseClient } from "@supabase/supabase-js";
|
|
4
|
+
|
|
5
|
+
//#region src/core/resolve-env.d.ts
|
|
6
|
+
/**
|
|
7
|
+
* Resolves Supabase environment configuration from runtime environment variables.
|
|
8
|
+
*
|
|
9
|
+
* Reads `SUPABASE_URL`, keys (`SUPABASE_PUBLISHABLE_KEYS` / `SUPABASE_SECRET_KEYS`),
|
|
10
|
+
* and `SUPABASE_JWKS`. Works across Deno, Node.js, and Bun. For Cloudflare Workers,
|
|
11
|
+
* use `overrides` or enable node-compat.
|
|
12
|
+
*
|
|
13
|
+
* @param overrides - Partial values that take precedence over env vars.
|
|
14
|
+
* @returns `{ data: SupabaseEnv, error: null }` on success, `{ data: null, error: EnvError }` on failure.
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* ```ts
|
|
18
|
+
* const { data: env, error } = resolveEnv()
|
|
19
|
+
* if (error) throw error
|
|
20
|
+
*
|
|
21
|
+
* // Override for tests
|
|
22
|
+
* const { data: env } = resolveEnv({ url: 'http://localhost:54321' })
|
|
23
|
+
* ```
|
|
24
|
+
*/
|
|
25
|
+
declare function resolveEnv(overrides?: Partial<SupabaseEnv>): {
|
|
26
|
+
data: SupabaseEnv;
|
|
27
|
+
error: null;
|
|
28
|
+
} | {
|
|
29
|
+
data: null;
|
|
30
|
+
error: EnvError;
|
|
31
|
+
};
|
|
32
|
+
//#endregion
|
|
33
|
+
//#region src/core/extract-credentials.d.ts
|
|
34
|
+
/**
|
|
35
|
+
* Extracts authentication credentials from an incoming HTTP request.
|
|
36
|
+
*
|
|
37
|
+
* Reads two headers:
|
|
38
|
+
* - `Authorization: Bearer <token>` → extracted as `token`
|
|
39
|
+
* - `apikey: <key>` → extracted as `apikey`
|
|
40
|
+
*
|
|
41
|
+
* This is a pure extraction step — no validation or verification is performed.
|
|
42
|
+
* Pass the result to {@link verifyCredentials} to validate against allowed auth modes.
|
|
43
|
+
*
|
|
44
|
+
* @param request - The incoming HTTP request.
|
|
45
|
+
* @returns The extracted {@link Credentials}. Fields are `null` when the corresponding header is absent.
|
|
46
|
+
*
|
|
47
|
+
* @example
|
|
48
|
+
* ```ts
|
|
49
|
+
* import { extractCredentials } from '@supabase/server/core'
|
|
50
|
+
*
|
|
51
|
+
* const creds = extractCredentials(request)
|
|
52
|
+
* console.log(creds.token) // "eyJhbGci..." or null
|
|
53
|
+
* console.log(creds.apikey) // "sb-abc123-publishable-..." or null
|
|
54
|
+
* ```
|
|
55
|
+
*/
|
|
56
|
+
declare function extractCredentials(request: Request): Credentials;
|
|
57
|
+
//#endregion
|
|
58
|
+
//#region src/core/verify-credentials.d.ts
|
|
59
|
+
/**
|
|
60
|
+
* Options for {@link verifyCredentials}.
|
|
61
|
+
*/
|
|
62
|
+
interface VerifyCredentialsOptions {
|
|
63
|
+
/**
|
|
64
|
+
* Auth mode(s) to try. Modes are attempted in order — the first match wins.
|
|
65
|
+
*
|
|
66
|
+
* @see {@link AllowWithKey} for the full syntax including named keys.
|
|
67
|
+
*/
|
|
68
|
+
allow: AllowWithKey | AllowWithKey[];
|
|
69
|
+
/** Optional environment overrides (passed through to {@link resolveEnv}). */
|
|
70
|
+
env?: Partial<SupabaseEnv>;
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Verifies pre-extracted credentials against one or more allowed auth modes.
|
|
74
|
+
*
|
|
75
|
+
* Tries each mode in order — first match wins. Use {@link verifyAuth} to extract
|
|
76
|
+
* and verify in a single call.
|
|
77
|
+
*
|
|
78
|
+
* @param credentials - The credentials to verify (from {@link extractCredentials}).
|
|
79
|
+
* @param options - Allowed auth modes and optional env overrides.
|
|
80
|
+
* @returns `{ data: AuthResult, error: null }` on success, `{ data: null, error: AuthError }` on failure.
|
|
81
|
+
*
|
|
82
|
+
* @example
|
|
83
|
+
* ```ts
|
|
84
|
+
* const credentials = extractCredentials(request)
|
|
85
|
+
* const { data: auth, error } = await verifyCredentials(credentials, {
|
|
86
|
+
* allow: ['user', 'public'],
|
|
87
|
+
* })
|
|
88
|
+
* if (error) {
|
|
89
|
+
* return Response.json({ error: error.message }, { status: error.status })
|
|
90
|
+
* }
|
|
91
|
+
* ```
|
|
92
|
+
*/
|
|
93
|
+
declare function verifyCredentials(credentials: Credentials, options: VerifyCredentialsOptions): Promise<{
|
|
94
|
+
data: AuthResult;
|
|
95
|
+
error: null;
|
|
96
|
+
} | {
|
|
97
|
+
data: null;
|
|
98
|
+
error: AuthError;
|
|
99
|
+
}>;
|
|
100
|
+
//#endregion
|
|
101
|
+
//#region src/core/verify-auth.d.ts
|
|
102
|
+
/**
|
|
103
|
+
* Options for {@link verifyAuth}.
|
|
104
|
+
*/
|
|
105
|
+
interface VerifyAuthOptions {
|
|
106
|
+
/**
|
|
107
|
+
* Auth mode(s) to try. Modes are attempted in order — the first match wins.
|
|
108
|
+
*
|
|
109
|
+
* @see {@link AllowWithKey} for the full syntax including named keys.
|
|
110
|
+
*/
|
|
111
|
+
allow: AllowWithKey | AllowWithKey[];
|
|
112
|
+
/** Optional environment overrides (passed through to {@link resolveEnv}). */
|
|
113
|
+
env?: Partial<SupabaseEnv>;
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* Extracts credentials from a request and verifies them in a single step.
|
|
117
|
+
*
|
|
118
|
+
* This is a convenience function that combines {@link extractCredentials} and
|
|
119
|
+
* {@link verifyCredentials}. Use it when you want the full auth flow without
|
|
120
|
+
* needing to inspect the raw credentials.
|
|
121
|
+
*
|
|
122
|
+
* @param request - The incoming HTTP request.
|
|
123
|
+
* @param options - Auth modes to accept and optional environment overrides.
|
|
124
|
+
*
|
|
125
|
+
* @returns A result tuple: `{ data, error }`.
|
|
126
|
+
* - On success: `{ data: AuthResult, error: null }`
|
|
127
|
+
* - On failure: `{ data: null, error: AuthError }`
|
|
128
|
+
*
|
|
129
|
+
* @example
|
|
130
|
+
* ```ts
|
|
131
|
+
* import { verifyAuth } from '@supabase/server/core'
|
|
132
|
+
*
|
|
133
|
+
* const { data: auth, error } = await verifyAuth(request, {
|
|
134
|
+
* allow: 'user',
|
|
135
|
+
* })
|
|
136
|
+
*
|
|
137
|
+
* if (error) {
|
|
138
|
+
* return Response.json({ error: error.message }, { status: error.status })
|
|
139
|
+
* }
|
|
140
|
+
*
|
|
141
|
+
* console.log(auth.userClaims!.id) // "d0f1a2b3-..."
|
|
142
|
+
* ```
|
|
143
|
+
*/
|
|
144
|
+
declare function verifyAuth(request: Request, options: VerifyAuthOptions): Promise<{
|
|
145
|
+
data: AuthResult;
|
|
146
|
+
error: null;
|
|
147
|
+
} | {
|
|
148
|
+
data: null;
|
|
149
|
+
error: AuthError;
|
|
150
|
+
}>;
|
|
151
|
+
//#endregion
|
|
152
|
+
//#region src/core/create-context-client.d.ts
|
|
153
|
+
/**
|
|
154
|
+
* Creates a Supabase client scoped to the caller's context.
|
|
155
|
+
*
|
|
156
|
+
* Configured with a publishable key and (optionally) the caller's JWT,
|
|
157
|
+
* so Row-Level Security policies apply. Session persistence is disabled
|
|
158
|
+
* (stateless, one client per request).
|
|
159
|
+
*
|
|
160
|
+
* @param token - The caller's JWT, or `null` for anonymous access.
|
|
161
|
+
* @param env - Optional environment overrides (passed through to {@link resolveEnv}).
|
|
162
|
+
* @param keyName - Name of the publishable key to use. Falls back to `"default"`, then first available.
|
|
163
|
+
* @returns A configured {@link SupabaseClient} with RLS enforced.
|
|
164
|
+
* @throws {@link EnvError} If `SUPABASE_URL` is missing or the specified publishable key is not found.
|
|
165
|
+
*
|
|
166
|
+
* @example
|
|
167
|
+
* ```ts
|
|
168
|
+
* const { data: auth } = await verifyAuth(request, { allow: 'user' })
|
|
169
|
+
* const supabase = createContextClient(auth.token)
|
|
170
|
+
* const { data } = await supabase.rpc('get_my_items')
|
|
171
|
+
* ```
|
|
172
|
+
*/
|
|
173
|
+
declare function createContextClient<Database = unknown>(token?: string | null, env?: Partial<SupabaseEnv>, keyName?: string | null): SupabaseClient<Database>;
|
|
174
|
+
//#endregion
|
|
175
|
+
//#region src/core/create-admin-client.d.ts
|
|
176
|
+
/**
|
|
177
|
+
* Creates an admin Supabase client that bypasses Row-Level Security.
|
|
178
|
+
*
|
|
179
|
+
* Uses a secret key for authentication, giving full access to all data.
|
|
180
|
+
* Session persistence is disabled (stateless, one client per request).
|
|
181
|
+
*
|
|
182
|
+
* @param env - Optional environment overrides (passed through to {@link resolveEnv}).
|
|
183
|
+
* @param keyName - Name of the secret key to use. Falls back to `"default"`, then first available.
|
|
184
|
+
* @returns A configured {@link SupabaseClient} with admin (service-role) privileges.
|
|
185
|
+
* @throws {@link EnvError} If `SUPABASE_URL` is missing or the specified secret key is not found.
|
|
186
|
+
*
|
|
187
|
+
* @example
|
|
188
|
+
* ```ts
|
|
189
|
+
* const supabaseAdmin = createAdminClient()
|
|
190
|
+
* const { data } = await supabaseAdmin.from('audit_log').insert({ action: 'user_login' })
|
|
191
|
+
* ```
|
|
192
|
+
*/
|
|
193
|
+
declare function createAdminClient<Database = unknown>(env?: Partial<SupabaseEnv>, keyName?: string | null): SupabaseClient<Database>;
|
|
194
|
+
//#endregion
|
|
3
195
|
export { createAdminClient, createContextClient, extractCredentials, resolveEnv, verifyAuth, verifyCredentials };
|
package/dist/core/index.d.mts
CHANGED
|
@@ -1,3 +1,195 @@
|
|
|
1
|
-
import "../types-ClmJ8pi8.mjs";
|
|
2
|
-
import {
|
|
1
|
+
import { i as Credentials, n as AllowWithKey, r as AuthResult, s as SupabaseEnv } from "../types-ClmJ8pi8.mjs";
|
|
2
|
+
import { n as EnvError, t as AuthError } from "../errors-5ivL23qo.mjs";
|
|
3
|
+
import { SupabaseClient } from "@supabase/supabase-js";
|
|
4
|
+
|
|
5
|
+
//#region src/core/resolve-env.d.ts
|
|
6
|
+
/**
|
|
7
|
+
* Resolves Supabase environment configuration from runtime environment variables.
|
|
8
|
+
*
|
|
9
|
+
* Reads `SUPABASE_URL`, keys (`SUPABASE_PUBLISHABLE_KEYS` / `SUPABASE_SECRET_KEYS`),
|
|
10
|
+
* and `SUPABASE_JWKS`. Works across Deno, Node.js, and Bun. For Cloudflare Workers,
|
|
11
|
+
* use `overrides` or enable node-compat.
|
|
12
|
+
*
|
|
13
|
+
* @param overrides - Partial values that take precedence over env vars.
|
|
14
|
+
* @returns `{ data: SupabaseEnv, error: null }` on success, `{ data: null, error: EnvError }` on failure.
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* ```ts
|
|
18
|
+
* const { data: env, error } = resolveEnv()
|
|
19
|
+
* if (error) throw error
|
|
20
|
+
*
|
|
21
|
+
* // Override for tests
|
|
22
|
+
* const { data: env } = resolveEnv({ url: 'http://localhost:54321' })
|
|
23
|
+
* ```
|
|
24
|
+
*/
|
|
25
|
+
declare function resolveEnv(overrides?: Partial<SupabaseEnv>): {
|
|
26
|
+
data: SupabaseEnv;
|
|
27
|
+
error: null;
|
|
28
|
+
} | {
|
|
29
|
+
data: null;
|
|
30
|
+
error: EnvError;
|
|
31
|
+
};
|
|
32
|
+
//#endregion
|
|
33
|
+
//#region src/core/extract-credentials.d.ts
|
|
34
|
+
/**
|
|
35
|
+
* Extracts authentication credentials from an incoming HTTP request.
|
|
36
|
+
*
|
|
37
|
+
* Reads two headers:
|
|
38
|
+
* - `Authorization: Bearer <token>` → extracted as `token`
|
|
39
|
+
* - `apikey: <key>` → extracted as `apikey`
|
|
40
|
+
*
|
|
41
|
+
* This is a pure extraction step — no validation or verification is performed.
|
|
42
|
+
* Pass the result to {@link verifyCredentials} to validate against allowed auth modes.
|
|
43
|
+
*
|
|
44
|
+
* @param request - The incoming HTTP request.
|
|
45
|
+
* @returns The extracted {@link Credentials}. Fields are `null` when the corresponding header is absent.
|
|
46
|
+
*
|
|
47
|
+
* @example
|
|
48
|
+
* ```ts
|
|
49
|
+
* import { extractCredentials } from '@supabase/server/core'
|
|
50
|
+
*
|
|
51
|
+
* const creds = extractCredentials(request)
|
|
52
|
+
* console.log(creds.token) // "eyJhbGci..." or null
|
|
53
|
+
* console.log(creds.apikey) // "sb-abc123-publishable-..." or null
|
|
54
|
+
* ```
|
|
55
|
+
*/
|
|
56
|
+
declare function extractCredentials(request: Request): Credentials;
|
|
57
|
+
//#endregion
|
|
58
|
+
//#region src/core/verify-credentials.d.ts
|
|
59
|
+
/**
|
|
60
|
+
* Options for {@link verifyCredentials}.
|
|
61
|
+
*/
|
|
62
|
+
interface VerifyCredentialsOptions {
|
|
63
|
+
/**
|
|
64
|
+
* Auth mode(s) to try. Modes are attempted in order — the first match wins.
|
|
65
|
+
*
|
|
66
|
+
* @see {@link AllowWithKey} for the full syntax including named keys.
|
|
67
|
+
*/
|
|
68
|
+
allow: AllowWithKey | AllowWithKey[];
|
|
69
|
+
/** Optional environment overrides (passed through to {@link resolveEnv}). */
|
|
70
|
+
env?: Partial<SupabaseEnv>;
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Verifies pre-extracted credentials against one or more allowed auth modes.
|
|
74
|
+
*
|
|
75
|
+
* Tries each mode in order — first match wins. Use {@link verifyAuth} to extract
|
|
76
|
+
* and verify in a single call.
|
|
77
|
+
*
|
|
78
|
+
* @param credentials - The credentials to verify (from {@link extractCredentials}).
|
|
79
|
+
* @param options - Allowed auth modes and optional env overrides.
|
|
80
|
+
* @returns `{ data: AuthResult, error: null }` on success, `{ data: null, error: AuthError }` on failure.
|
|
81
|
+
*
|
|
82
|
+
* @example
|
|
83
|
+
* ```ts
|
|
84
|
+
* const credentials = extractCredentials(request)
|
|
85
|
+
* const { data: auth, error } = await verifyCredentials(credentials, {
|
|
86
|
+
* allow: ['user', 'public'],
|
|
87
|
+
* })
|
|
88
|
+
* if (error) {
|
|
89
|
+
* return Response.json({ error: error.message }, { status: error.status })
|
|
90
|
+
* }
|
|
91
|
+
* ```
|
|
92
|
+
*/
|
|
93
|
+
declare function verifyCredentials(credentials: Credentials, options: VerifyCredentialsOptions): Promise<{
|
|
94
|
+
data: AuthResult;
|
|
95
|
+
error: null;
|
|
96
|
+
} | {
|
|
97
|
+
data: null;
|
|
98
|
+
error: AuthError;
|
|
99
|
+
}>;
|
|
100
|
+
//#endregion
|
|
101
|
+
//#region src/core/verify-auth.d.ts
|
|
102
|
+
/**
|
|
103
|
+
* Options for {@link verifyAuth}.
|
|
104
|
+
*/
|
|
105
|
+
interface VerifyAuthOptions {
|
|
106
|
+
/**
|
|
107
|
+
* Auth mode(s) to try. Modes are attempted in order — the first match wins.
|
|
108
|
+
*
|
|
109
|
+
* @see {@link AllowWithKey} for the full syntax including named keys.
|
|
110
|
+
*/
|
|
111
|
+
allow: AllowWithKey | AllowWithKey[];
|
|
112
|
+
/** Optional environment overrides (passed through to {@link resolveEnv}). */
|
|
113
|
+
env?: Partial<SupabaseEnv>;
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* Extracts credentials from a request and verifies them in a single step.
|
|
117
|
+
*
|
|
118
|
+
* This is a convenience function that combines {@link extractCredentials} and
|
|
119
|
+
* {@link verifyCredentials}. Use it when you want the full auth flow without
|
|
120
|
+
* needing to inspect the raw credentials.
|
|
121
|
+
*
|
|
122
|
+
* @param request - The incoming HTTP request.
|
|
123
|
+
* @param options - Auth modes to accept and optional environment overrides.
|
|
124
|
+
*
|
|
125
|
+
* @returns A result tuple: `{ data, error }`.
|
|
126
|
+
* - On success: `{ data: AuthResult, error: null }`
|
|
127
|
+
* - On failure: `{ data: null, error: AuthError }`
|
|
128
|
+
*
|
|
129
|
+
* @example
|
|
130
|
+
* ```ts
|
|
131
|
+
* import { verifyAuth } from '@supabase/server/core'
|
|
132
|
+
*
|
|
133
|
+
* const { data: auth, error } = await verifyAuth(request, {
|
|
134
|
+
* allow: 'user',
|
|
135
|
+
* })
|
|
136
|
+
*
|
|
137
|
+
* if (error) {
|
|
138
|
+
* return Response.json({ error: error.message }, { status: error.status })
|
|
139
|
+
* }
|
|
140
|
+
*
|
|
141
|
+
* console.log(auth.userClaims!.id) // "d0f1a2b3-..."
|
|
142
|
+
* ```
|
|
143
|
+
*/
|
|
144
|
+
declare function verifyAuth(request: Request, options: VerifyAuthOptions): Promise<{
|
|
145
|
+
data: AuthResult;
|
|
146
|
+
error: null;
|
|
147
|
+
} | {
|
|
148
|
+
data: null;
|
|
149
|
+
error: AuthError;
|
|
150
|
+
}>;
|
|
151
|
+
//#endregion
|
|
152
|
+
//#region src/core/create-context-client.d.ts
|
|
153
|
+
/**
|
|
154
|
+
* Creates a Supabase client scoped to the caller's context.
|
|
155
|
+
*
|
|
156
|
+
* Configured with a publishable key and (optionally) the caller's JWT,
|
|
157
|
+
* so Row-Level Security policies apply. Session persistence is disabled
|
|
158
|
+
* (stateless, one client per request).
|
|
159
|
+
*
|
|
160
|
+
* @param token - The caller's JWT, or `null` for anonymous access.
|
|
161
|
+
* @param env - Optional environment overrides (passed through to {@link resolveEnv}).
|
|
162
|
+
* @param keyName - Name of the publishable key to use. Falls back to `"default"`, then first available.
|
|
163
|
+
* @returns A configured {@link SupabaseClient} with RLS enforced.
|
|
164
|
+
* @throws {@link EnvError} If `SUPABASE_URL` is missing or the specified publishable key is not found.
|
|
165
|
+
*
|
|
166
|
+
* @example
|
|
167
|
+
* ```ts
|
|
168
|
+
* const { data: auth } = await verifyAuth(request, { allow: 'user' })
|
|
169
|
+
* const supabase = createContextClient(auth.token)
|
|
170
|
+
* const { data } = await supabase.rpc('get_my_items')
|
|
171
|
+
* ```
|
|
172
|
+
*/
|
|
173
|
+
declare function createContextClient<Database = unknown>(token?: string | null, env?: Partial<SupabaseEnv>, keyName?: string | null): SupabaseClient<Database>;
|
|
174
|
+
//#endregion
|
|
175
|
+
//#region src/core/create-admin-client.d.ts
|
|
176
|
+
/**
|
|
177
|
+
* Creates an admin Supabase client that bypasses Row-Level Security.
|
|
178
|
+
*
|
|
179
|
+
* Uses a secret key for authentication, giving full access to all data.
|
|
180
|
+
* Session persistence is disabled (stateless, one client per request).
|
|
181
|
+
*
|
|
182
|
+
* @param env - Optional environment overrides (passed through to {@link resolveEnv}).
|
|
183
|
+
* @param keyName - Name of the secret key to use. Falls back to `"default"`, then first available.
|
|
184
|
+
* @returns A configured {@link SupabaseClient} with admin (service-role) privileges.
|
|
185
|
+
* @throws {@link EnvError} If `SUPABASE_URL` is missing or the specified secret key is not found.
|
|
186
|
+
*
|
|
187
|
+
* @example
|
|
188
|
+
* ```ts
|
|
189
|
+
* const supabaseAdmin = createAdminClient()
|
|
190
|
+
* const { data } = await supabaseAdmin.from('audit_log').insert({ action: 'user_login' })
|
|
191
|
+
* ```
|
|
192
|
+
*/
|
|
193
|
+
declare function createAdminClient<Database = unknown>(env?: Partial<SupabaseEnv>, keyName?: string | null): SupabaseClient<Database>;
|
|
194
|
+
//#endregion
|
|
3
195
|
export { createAdminClient, createContextClient, extractCredentials, resolveEnv, verifyAuth, verifyCredentials };
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
//#region src/errors.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* Thrown when a required environment variable is missing or malformed.
|
|
4
|
+
*
|
|
5
|
+
* Has a fixed `status` of `500` since environment errors are server-side
|
|
6
|
+
* configuration issues, not client errors.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* ```ts
|
|
10
|
+
* import { EnvError } from '@supabase/server'
|
|
11
|
+
*
|
|
12
|
+
* try {
|
|
13
|
+
* const client = createAdminClient()
|
|
14
|
+
* } catch (e) {
|
|
15
|
+
* if (e instanceof EnvError) {
|
|
16
|
+
* console.error(`Config issue [${e.code}]: ${e.message}`)
|
|
17
|
+
* // → "Config issue [MISSING_SUPABASE_URL]: SUPABASE_URL is required but not set"
|
|
18
|
+
* }
|
|
19
|
+
* }
|
|
20
|
+
* ```
|
|
21
|
+
*/
|
|
22
|
+
declare class EnvError extends Error {
|
|
23
|
+
/** Always `500` — environment errors are server-side issues. */
|
|
24
|
+
readonly status = 500;
|
|
25
|
+
/**
|
|
26
|
+
* Machine-readable error code.
|
|
27
|
+
*
|
|
28
|
+
* Known codes:
|
|
29
|
+
* - `"MISSING_SUPABASE_URL"` — `SUPABASE_URL` not set
|
|
30
|
+
* - `"MISSING_PUBLISHABLE_KEY"` — No publishable key found
|
|
31
|
+
* - `"MISSING_SECRET_KEY"` — No secret key found
|
|
32
|
+
* - `"ENV_ERROR"` — Generic environment error
|
|
33
|
+
*/
|
|
34
|
+
readonly code: string;
|
|
35
|
+
constructor(message: string, code?: string);
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Thrown when authentication or authorization fails.
|
|
39
|
+
*
|
|
40
|
+
* Carries an HTTP `status` code suitable for returning directly in a response
|
|
41
|
+
* (typically `401` for invalid credentials, `500` for server-side auth failures).
|
|
42
|
+
*
|
|
43
|
+
* @example
|
|
44
|
+
* ```ts
|
|
45
|
+
* import { AuthError, createSupabaseContext } from '@supabase/server'
|
|
46
|
+
*
|
|
47
|
+
* const { data: ctx, error } = await createSupabaseContext(request, { allow: 'user' })
|
|
48
|
+
* if (error) {
|
|
49
|
+
* // error is an AuthError
|
|
50
|
+
* return Response.json(
|
|
51
|
+
* { error: error.message, code: error.code },
|
|
52
|
+
* { status: error.status },
|
|
53
|
+
* )
|
|
54
|
+
* }
|
|
55
|
+
* ```
|
|
56
|
+
*/
|
|
57
|
+
declare class AuthError extends Error {
|
|
58
|
+
/**
|
|
59
|
+
* HTTP status code.
|
|
60
|
+
*
|
|
61
|
+
* - `401` — Invalid or missing credentials
|
|
62
|
+
* - `500` — Server-side auth failure (e.g., missing JWKS, env misconfiguration)
|
|
63
|
+
*/
|
|
64
|
+
readonly status: number;
|
|
65
|
+
/**
|
|
66
|
+
* Machine-readable error code.
|
|
67
|
+
*
|
|
68
|
+
* Known codes:
|
|
69
|
+
* - `"INVALID_CREDENTIALS"` — No credential matched any allowed auth mode
|
|
70
|
+
* - `"CLIENT_ERROR"` — Failed to create a Supabase client after auth succeeded
|
|
71
|
+
* - `"AUTH_ERROR"` — Generic authentication error
|
|
72
|
+
* - Any `EnvError` code (propagated when env resolution fails during auth)
|
|
73
|
+
*/
|
|
74
|
+
readonly code: string;
|
|
75
|
+
constructor(message: string, code?: string, status?: number);
|
|
76
|
+
}
|
|
77
|
+
//#endregion
|
|
78
|
+
export { EnvError as n, AuthError as t };
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
//#region src/errors.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* Thrown when a required environment variable is missing or malformed.
|
|
4
|
+
*
|
|
5
|
+
* Has a fixed `status` of `500` since environment errors are server-side
|
|
6
|
+
* configuration issues, not client errors.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* ```ts
|
|
10
|
+
* import { EnvError } from '@supabase/server'
|
|
11
|
+
*
|
|
12
|
+
* try {
|
|
13
|
+
* const client = createAdminClient()
|
|
14
|
+
* } catch (e) {
|
|
15
|
+
* if (e instanceof EnvError) {
|
|
16
|
+
* console.error(`Config issue [${e.code}]: ${e.message}`)
|
|
17
|
+
* // → "Config issue [MISSING_SUPABASE_URL]: SUPABASE_URL is required but not set"
|
|
18
|
+
* }
|
|
19
|
+
* }
|
|
20
|
+
* ```
|
|
21
|
+
*/
|
|
22
|
+
declare class EnvError extends Error {
|
|
23
|
+
/** Always `500` — environment errors are server-side issues. */
|
|
24
|
+
readonly status = 500;
|
|
25
|
+
/**
|
|
26
|
+
* Machine-readable error code.
|
|
27
|
+
*
|
|
28
|
+
* Known codes:
|
|
29
|
+
* - `"MISSING_SUPABASE_URL"` — `SUPABASE_URL` not set
|
|
30
|
+
* - `"MISSING_PUBLISHABLE_KEY"` — No publishable key found
|
|
31
|
+
* - `"MISSING_SECRET_KEY"` — No secret key found
|
|
32
|
+
* - `"ENV_ERROR"` — Generic environment error
|
|
33
|
+
*/
|
|
34
|
+
readonly code: string;
|
|
35
|
+
constructor(message: string, code?: string);
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Thrown when authentication or authorization fails.
|
|
39
|
+
*
|
|
40
|
+
* Carries an HTTP `status` code suitable for returning directly in a response
|
|
41
|
+
* (typically `401` for invalid credentials, `500` for server-side auth failures).
|
|
42
|
+
*
|
|
43
|
+
* @example
|
|
44
|
+
* ```ts
|
|
45
|
+
* import { AuthError, createSupabaseContext } from '@supabase/server'
|
|
46
|
+
*
|
|
47
|
+
* const { data: ctx, error } = await createSupabaseContext(request, { allow: 'user' })
|
|
48
|
+
* if (error) {
|
|
49
|
+
* // error is an AuthError
|
|
50
|
+
* return Response.json(
|
|
51
|
+
* { error: error.message, code: error.code },
|
|
52
|
+
* { status: error.status },
|
|
53
|
+
* )
|
|
54
|
+
* }
|
|
55
|
+
* ```
|
|
56
|
+
*/
|
|
57
|
+
declare class AuthError extends Error {
|
|
58
|
+
/**
|
|
59
|
+
* HTTP status code.
|
|
60
|
+
*
|
|
61
|
+
* - `401` — Invalid or missing credentials
|
|
62
|
+
* - `500` — Server-side auth failure (e.g., missing JWKS, env misconfiguration)
|
|
63
|
+
*/
|
|
64
|
+
readonly status: number;
|
|
65
|
+
/**
|
|
66
|
+
* Machine-readable error code.
|
|
67
|
+
*
|
|
68
|
+
* Known codes:
|
|
69
|
+
* - `"INVALID_CREDENTIALS"` — No credential matched any allowed auth mode
|
|
70
|
+
* - `"CLIENT_ERROR"` — Failed to create a Supabase client after auth succeeded
|
|
71
|
+
* - `"AUTH_ERROR"` — Generic authentication error
|
|
72
|
+
* - Any `EnvError` code (propagated when env resolution fails during auth)
|
|
73
|
+
*/
|
|
74
|
+
readonly code: string;
|
|
75
|
+
constructor(message: string, code?: string, status?: number);
|
|
76
|
+
}
|
|
77
|
+
//#endregion
|
|
78
|
+
export { EnvError as n, AuthError as t };
|
package/dist/index.cjs
CHANGED
|
@@ -85,11 +85,5 @@ function withSupabase(config, handler) {
|
|
|
85
85
|
//#endregion
|
|
86
86
|
exports.AuthError = require_verify_auth.AuthError;
|
|
87
87
|
exports.EnvError = require_verify_auth.EnvError;
|
|
88
|
-
exports.createAdminClient = require_verify_auth.createAdminClient;
|
|
89
|
-
exports.createContextClient = require_verify_auth.createContextClient;
|
|
90
88
|
exports.createSupabaseContext = require_create_supabase_context.createSupabaseContext;
|
|
91
|
-
exports.extractCredentials = require_verify_auth.extractCredentials;
|
|
92
|
-
exports.resolveEnv = require_verify_auth.resolveEnv;
|
|
93
|
-
exports.verifyAuth = require_verify_auth.verifyAuth;
|
|
94
|
-
exports.verifyCredentials = require_verify_auth.verifyCredentials;
|
|
95
89
|
exports.withSupabase = withSupabase;
|
package/dist/index.d.cts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { a as JWTClaims, c as UserClaims, i as Credentials, l as WithSupabaseConfig, n as AllowWithKey, o as SupabaseContext, r as AuthResult, s as SupabaseEnv, t as Allow } from "./types-CnKoFCMX.cjs";
|
|
2
|
-
import {
|
|
2
|
+
import { n as EnvError, t as AuthError } from "./errors-BmSsOAvx.cjs";
|
|
3
3
|
|
|
4
4
|
//#region src/with-supabase.d.ts
|
|
5
5
|
/**
|
|
@@ -56,4 +56,4 @@ declare function createSupabaseContext<Database = unknown>(request: Request, opt
|
|
|
56
56
|
error: AuthError;
|
|
57
57
|
}>;
|
|
58
58
|
//#endregion
|
|
59
|
-
export { type Allow, type AllowWithKey, AuthError, type AuthResult, type Credentials, EnvError, type JWTClaims, type SupabaseContext, type SupabaseEnv, type UserClaims, type WithSupabaseConfig,
|
|
59
|
+
export { type Allow, type AllowWithKey, AuthError, type AuthResult, type Credentials, EnvError, type JWTClaims, type SupabaseContext, type SupabaseEnv, type UserClaims, type WithSupabaseConfig, createSupabaseContext, withSupabase };
|
package/dist/index.d.mts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { a as JWTClaims, c as UserClaims, i as Credentials, l as WithSupabaseConfig, n as AllowWithKey, o as SupabaseContext, r as AuthResult, s as SupabaseEnv, t as Allow } from "./types-ClmJ8pi8.mjs";
|
|
2
|
-
import {
|
|
2
|
+
import { n as EnvError, t as AuthError } from "./errors-5ivL23qo.mjs";
|
|
3
3
|
|
|
4
4
|
//#region src/with-supabase.d.ts
|
|
5
5
|
/**
|
|
@@ -56,4 +56,4 @@ declare function createSupabaseContext<Database = unknown>(request: Request, opt
|
|
|
56
56
|
error: AuthError;
|
|
57
57
|
}>;
|
|
58
58
|
//#endregion
|
|
59
|
-
export { type Allow, type AllowWithKey, AuthError, type AuthResult, type Credentials, EnvError, type JWTClaims, type SupabaseContext, type SupabaseEnv, type UserClaims, type WithSupabaseConfig,
|
|
59
|
+
export { type Allow, type AllowWithKey, AuthError, type AuthResult, type Credentials, EnvError, type JWTClaims, type SupabaseContext, type SupabaseEnv, type UserClaims, type WithSupabaseConfig, createSupabaseContext, withSupabase };
|
package/dist/index.mjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { c as EnvError, s as AuthError } from "./verify-auth-2S7zFfR-.mjs";
|
|
2
2
|
import { t as createSupabaseContext } from "./create-supabase-context-CmWaH3s6.mjs";
|
|
3
3
|
import { corsHeaders } from "@supabase/supabase-js/cors";
|
|
4
4
|
|
|
@@ -82,4 +82,4 @@ function withSupabase(config, handler) {
|
|
|
82
82
|
}
|
|
83
83
|
|
|
84
84
|
//#endregion
|
|
85
|
-
export { AuthError, EnvError,
|
|
85
|
+
export { AuthError, EnvError, createSupabaseContext, withSupabase };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@supabase/server",
|
|
3
|
-
"version": "0.1.1-rc.
|
|
3
|
+
"version": "0.1.1-rc.26",
|
|
4
4
|
"description": "Server-side utilities for Supabase. Handles auth, client creation, and context injection so you write business logic, not boilerplate.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"edge",
|
|
@@ -1,271 +0,0 @@
|
|
|
1
|
-
import { i as Credentials, n as AllowWithKey, r as AuthResult, s as SupabaseEnv } from "./types-CnKoFCMX.cjs";
|
|
2
|
-
import { SupabaseClient } from "@supabase/supabase-js";
|
|
3
|
-
|
|
4
|
-
//#region src/errors.d.ts
|
|
5
|
-
/**
|
|
6
|
-
* Thrown when a required environment variable is missing or malformed.
|
|
7
|
-
*
|
|
8
|
-
* Has a fixed `status` of `500` since environment errors are server-side
|
|
9
|
-
* configuration issues, not client errors.
|
|
10
|
-
*
|
|
11
|
-
* @example
|
|
12
|
-
* ```ts
|
|
13
|
-
* import { EnvError } from '@supabase/server'
|
|
14
|
-
*
|
|
15
|
-
* try {
|
|
16
|
-
* const client = createAdminClient()
|
|
17
|
-
* } catch (e) {
|
|
18
|
-
* if (e instanceof EnvError) {
|
|
19
|
-
* console.error(`Config issue [${e.code}]: ${e.message}`)
|
|
20
|
-
* // → "Config issue [MISSING_SUPABASE_URL]: SUPABASE_URL is required but not set"
|
|
21
|
-
* }
|
|
22
|
-
* }
|
|
23
|
-
* ```
|
|
24
|
-
*/
|
|
25
|
-
declare class EnvError extends Error {
|
|
26
|
-
/** Always `500` — environment errors are server-side issues. */
|
|
27
|
-
readonly status = 500;
|
|
28
|
-
/**
|
|
29
|
-
* Machine-readable error code.
|
|
30
|
-
*
|
|
31
|
-
* Known codes:
|
|
32
|
-
* - `"MISSING_SUPABASE_URL"` — `SUPABASE_URL` not set
|
|
33
|
-
* - `"MISSING_PUBLISHABLE_KEY"` — No publishable key found
|
|
34
|
-
* - `"MISSING_SECRET_KEY"` — No secret key found
|
|
35
|
-
* - `"ENV_ERROR"` — Generic environment error
|
|
36
|
-
*/
|
|
37
|
-
readonly code: string;
|
|
38
|
-
constructor(message: string, code?: string);
|
|
39
|
-
}
|
|
40
|
-
/**
|
|
41
|
-
* Thrown when authentication or authorization fails.
|
|
42
|
-
*
|
|
43
|
-
* Carries an HTTP `status` code suitable for returning directly in a response
|
|
44
|
-
* (typically `401` for invalid credentials, `500` for server-side auth failures).
|
|
45
|
-
*
|
|
46
|
-
* @example
|
|
47
|
-
* ```ts
|
|
48
|
-
* import { AuthError, createSupabaseContext } from '@supabase/server'
|
|
49
|
-
*
|
|
50
|
-
* const { data: ctx, error } = await createSupabaseContext(request, { allow: 'user' })
|
|
51
|
-
* if (error) {
|
|
52
|
-
* // error is an AuthError
|
|
53
|
-
* return Response.json(
|
|
54
|
-
* { error: error.message, code: error.code },
|
|
55
|
-
* { status: error.status },
|
|
56
|
-
* )
|
|
57
|
-
* }
|
|
58
|
-
* ```
|
|
59
|
-
*/
|
|
60
|
-
declare class AuthError extends Error {
|
|
61
|
-
/**
|
|
62
|
-
* HTTP status code.
|
|
63
|
-
*
|
|
64
|
-
* - `401` — Invalid or missing credentials
|
|
65
|
-
* - `500` — Server-side auth failure (e.g., missing JWKS, env misconfiguration)
|
|
66
|
-
*/
|
|
67
|
-
readonly status: number;
|
|
68
|
-
/**
|
|
69
|
-
* Machine-readable error code.
|
|
70
|
-
*
|
|
71
|
-
* Known codes:
|
|
72
|
-
* - `"INVALID_CREDENTIALS"` — No credential matched any allowed auth mode
|
|
73
|
-
* - `"CLIENT_ERROR"` — Failed to create a Supabase client after auth succeeded
|
|
74
|
-
* - `"AUTH_ERROR"` — Generic authentication error
|
|
75
|
-
* - Any `EnvError` code (propagated when env resolution fails during auth)
|
|
76
|
-
*/
|
|
77
|
-
readonly code: string;
|
|
78
|
-
constructor(message: string, code?: string, status?: number);
|
|
79
|
-
}
|
|
80
|
-
//#endregion
|
|
81
|
-
//#region src/core/resolve-env.d.ts
|
|
82
|
-
/**
|
|
83
|
-
* Resolves Supabase environment configuration from runtime environment variables.
|
|
84
|
-
*
|
|
85
|
-
* Reads `SUPABASE_URL`, keys (`SUPABASE_PUBLISHABLE_KEYS` / `SUPABASE_SECRET_KEYS`),
|
|
86
|
-
* and `SUPABASE_JWKS`. Works across Deno, Node.js, and Bun. For Cloudflare Workers,
|
|
87
|
-
* use `overrides` or enable node-compat.
|
|
88
|
-
*
|
|
89
|
-
* @param overrides - Partial values that take precedence over env vars.
|
|
90
|
-
* @returns `{ data: SupabaseEnv, error: null }` on success, `{ data: null, error: EnvError }` on failure.
|
|
91
|
-
*
|
|
92
|
-
* @example
|
|
93
|
-
* ```ts
|
|
94
|
-
* const { data: env, error } = resolveEnv()
|
|
95
|
-
* if (error) throw error
|
|
96
|
-
*
|
|
97
|
-
* // Override for tests
|
|
98
|
-
* const { data: env } = resolveEnv({ url: 'http://localhost:54321' })
|
|
99
|
-
* ```
|
|
100
|
-
*/
|
|
101
|
-
declare function resolveEnv(overrides?: Partial<SupabaseEnv>): {
|
|
102
|
-
data: SupabaseEnv;
|
|
103
|
-
error: null;
|
|
104
|
-
} | {
|
|
105
|
-
data: null;
|
|
106
|
-
error: EnvError;
|
|
107
|
-
};
|
|
108
|
-
//#endregion
|
|
109
|
-
//#region src/core/extract-credentials.d.ts
|
|
110
|
-
/**
|
|
111
|
-
* Extracts authentication credentials from an incoming HTTP request.
|
|
112
|
-
*
|
|
113
|
-
* Reads two headers:
|
|
114
|
-
* - `Authorization: Bearer <token>` → extracted as `token`
|
|
115
|
-
* - `apikey: <key>` → extracted as `apikey`
|
|
116
|
-
*
|
|
117
|
-
* This is a pure extraction step — no validation or verification is performed.
|
|
118
|
-
* Pass the result to {@link verifyCredentials} to validate against allowed auth modes.
|
|
119
|
-
*
|
|
120
|
-
* @param request - The incoming HTTP request.
|
|
121
|
-
* @returns The extracted {@link Credentials}. Fields are `null` when the corresponding header is absent.
|
|
122
|
-
*
|
|
123
|
-
* @example
|
|
124
|
-
* ```ts
|
|
125
|
-
* import { extractCredentials } from '@supabase/server/core'
|
|
126
|
-
*
|
|
127
|
-
* const creds = extractCredentials(request)
|
|
128
|
-
* console.log(creds.token) // "eyJhbGci..." or null
|
|
129
|
-
* console.log(creds.apikey) // "sb-abc123-publishable-..." or null
|
|
130
|
-
* ```
|
|
131
|
-
*/
|
|
132
|
-
declare function extractCredentials(request: Request): Credentials;
|
|
133
|
-
//#endregion
|
|
134
|
-
//#region src/core/verify-credentials.d.ts
|
|
135
|
-
/**
|
|
136
|
-
* Options for {@link verifyCredentials}.
|
|
137
|
-
*/
|
|
138
|
-
interface VerifyCredentialsOptions {
|
|
139
|
-
/**
|
|
140
|
-
* Auth mode(s) to try. Modes are attempted in order — the first match wins.
|
|
141
|
-
*
|
|
142
|
-
* @see {@link AllowWithKey} for the full syntax including named keys.
|
|
143
|
-
*/
|
|
144
|
-
allow: AllowWithKey | AllowWithKey[];
|
|
145
|
-
/** Optional environment overrides (passed through to {@link resolveEnv}). */
|
|
146
|
-
env?: Partial<SupabaseEnv>;
|
|
147
|
-
}
|
|
148
|
-
/**
|
|
149
|
-
* Verifies pre-extracted credentials against one or more allowed auth modes.
|
|
150
|
-
*
|
|
151
|
-
* Tries each mode in order — first match wins. Use {@link verifyAuth} to extract
|
|
152
|
-
* and verify in a single call.
|
|
153
|
-
*
|
|
154
|
-
* @param credentials - The credentials to verify (from {@link extractCredentials}).
|
|
155
|
-
* @param options - Allowed auth modes and optional env overrides.
|
|
156
|
-
* @returns `{ data: AuthResult, error: null }` on success, `{ data: null, error: AuthError }` on failure.
|
|
157
|
-
*
|
|
158
|
-
* @example
|
|
159
|
-
* ```ts
|
|
160
|
-
* const credentials = extractCredentials(request)
|
|
161
|
-
* const { data: auth, error } = await verifyCredentials(credentials, {
|
|
162
|
-
* allow: ['user', 'public'],
|
|
163
|
-
* })
|
|
164
|
-
* if (error) {
|
|
165
|
-
* return Response.json({ error: error.message }, { status: error.status })
|
|
166
|
-
* }
|
|
167
|
-
* ```
|
|
168
|
-
*/
|
|
169
|
-
declare function verifyCredentials(credentials: Credentials, options: VerifyCredentialsOptions): Promise<{
|
|
170
|
-
data: AuthResult;
|
|
171
|
-
error: null;
|
|
172
|
-
} | {
|
|
173
|
-
data: null;
|
|
174
|
-
error: AuthError;
|
|
175
|
-
}>;
|
|
176
|
-
//#endregion
|
|
177
|
-
//#region src/core/verify-auth.d.ts
|
|
178
|
-
/**
|
|
179
|
-
* Options for {@link verifyAuth}.
|
|
180
|
-
*/
|
|
181
|
-
interface VerifyAuthOptions {
|
|
182
|
-
/**
|
|
183
|
-
* Auth mode(s) to try. Modes are attempted in order — the first match wins.
|
|
184
|
-
*
|
|
185
|
-
* @see {@link AllowWithKey} for the full syntax including named keys.
|
|
186
|
-
*/
|
|
187
|
-
allow: AllowWithKey | AllowWithKey[];
|
|
188
|
-
/** Optional environment overrides (passed through to {@link resolveEnv}). */
|
|
189
|
-
env?: Partial<SupabaseEnv>;
|
|
190
|
-
}
|
|
191
|
-
/**
|
|
192
|
-
* Extracts credentials from a request and verifies them in a single step.
|
|
193
|
-
*
|
|
194
|
-
* This is a convenience function that combines {@link extractCredentials} and
|
|
195
|
-
* {@link verifyCredentials}. Use it when you want the full auth flow without
|
|
196
|
-
* needing to inspect the raw credentials.
|
|
197
|
-
*
|
|
198
|
-
* @param request - The incoming HTTP request.
|
|
199
|
-
* @param options - Auth modes to accept and optional environment overrides.
|
|
200
|
-
*
|
|
201
|
-
* @returns A result tuple: `{ data, error }`.
|
|
202
|
-
* - On success: `{ data: AuthResult, error: null }`
|
|
203
|
-
* - On failure: `{ data: null, error: AuthError }`
|
|
204
|
-
*
|
|
205
|
-
* @example
|
|
206
|
-
* ```ts
|
|
207
|
-
* import { verifyAuth } from '@supabase/server/core'
|
|
208
|
-
*
|
|
209
|
-
* const { data: auth, error } = await verifyAuth(request, {
|
|
210
|
-
* allow: 'user',
|
|
211
|
-
* })
|
|
212
|
-
*
|
|
213
|
-
* if (error) {
|
|
214
|
-
* return Response.json({ error: error.message }, { status: error.status })
|
|
215
|
-
* }
|
|
216
|
-
*
|
|
217
|
-
* console.log(auth.userClaims!.id) // "d0f1a2b3-..."
|
|
218
|
-
* ```
|
|
219
|
-
*/
|
|
220
|
-
declare function verifyAuth(request: Request, options: VerifyAuthOptions): Promise<{
|
|
221
|
-
data: AuthResult;
|
|
222
|
-
error: null;
|
|
223
|
-
} | {
|
|
224
|
-
data: null;
|
|
225
|
-
error: AuthError;
|
|
226
|
-
}>;
|
|
227
|
-
//#endregion
|
|
228
|
-
//#region src/core/create-context-client.d.ts
|
|
229
|
-
/**
|
|
230
|
-
* Creates a Supabase client scoped to the caller's context.
|
|
231
|
-
*
|
|
232
|
-
* Configured with a publishable key and (optionally) the caller's JWT,
|
|
233
|
-
* so Row-Level Security policies apply. Session persistence is disabled
|
|
234
|
-
* (stateless, one client per request).
|
|
235
|
-
*
|
|
236
|
-
* @param token - The caller's JWT, or `null` for anonymous access.
|
|
237
|
-
* @param env - Optional environment overrides (passed through to {@link resolveEnv}).
|
|
238
|
-
* @param keyName - Name of the publishable key to use. Falls back to `"default"`, then first available.
|
|
239
|
-
* @returns A configured {@link SupabaseClient} with RLS enforced.
|
|
240
|
-
* @throws {@link EnvError} If `SUPABASE_URL` is missing or the specified publishable key is not found.
|
|
241
|
-
*
|
|
242
|
-
* @example
|
|
243
|
-
* ```ts
|
|
244
|
-
* const { data: auth } = await verifyAuth(request, { allow: 'user' })
|
|
245
|
-
* const supabase = createContextClient(auth.token)
|
|
246
|
-
* const { data } = await supabase.rpc('get_my_items')
|
|
247
|
-
* ```
|
|
248
|
-
*/
|
|
249
|
-
declare function createContextClient<Database = unknown>(token?: string | null, env?: Partial<SupabaseEnv>, keyName?: string | null): SupabaseClient<Database>;
|
|
250
|
-
//#endregion
|
|
251
|
-
//#region src/core/create-admin-client.d.ts
|
|
252
|
-
/**
|
|
253
|
-
* Creates an admin Supabase client that bypasses Row-Level Security.
|
|
254
|
-
*
|
|
255
|
-
* Uses a secret key for authentication, giving full access to all data.
|
|
256
|
-
* Session persistence is disabled (stateless, one client per request).
|
|
257
|
-
*
|
|
258
|
-
* @param env - Optional environment overrides (passed through to {@link resolveEnv}).
|
|
259
|
-
* @param keyName - Name of the secret key to use. Falls back to `"default"`, then first available.
|
|
260
|
-
* @returns A configured {@link SupabaseClient} with admin (service-role) privileges.
|
|
261
|
-
* @throws {@link EnvError} If `SUPABASE_URL` is missing or the specified secret key is not found.
|
|
262
|
-
*
|
|
263
|
-
* @example
|
|
264
|
-
* ```ts
|
|
265
|
-
* const supabaseAdmin = createAdminClient()
|
|
266
|
-
* const { data } = await supabaseAdmin.from('audit_log').insert({ action: 'user_login' })
|
|
267
|
-
* ```
|
|
268
|
-
*/
|
|
269
|
-
declare function createAdminClient<Database = unknown>(env?: Partial<SupabaseEnv>, keyName?: string | null): SupabaseClient<Database>;
|
|
270
|
-
//#endregion
|
|
271
|
-
export { extractCredentials as a, EnvError as c, verifyCredentials as i, createContextClient as n, resolveEnv as o, verifyAuth as r, AuthError as s, createAdminClient as t };
|
|
@@ -1,271 +0,0 @@
|
|
|
1
|
-
import { i as Credentials, n as AllowWithKey, r as AuthResult, s as SupabaseEnv } from "./types-ClmJ8pi8.mjs";
|
|
2
|
-
import { SupabaseClient } from "@supabase/supabase-js";
|
|
3
|
-
|
|
4
|
-
//#region src/errors.d.ts
|
|
5
|
-
/**
|
|
6
|
-
* Thrown when a required environment variable is missing or malformed.
|
|
7
|
-
*
|
|
8
|
-
* Has a fixed `status` of `500` since environment errors are server-side
|
|
9
|
-
* configuration issues, not client errors.
|
|
10
|
-
*
|
|
11
|
-
* @example
|
|
12
|
-
* ```ts
|
|
13
|
-
* import { EnvError } from '@supabase/server'
|
|
14
|
-
*
|
|
15
|
-
* try {
|
|
16
|
-
* const client = createAdminClient()
|
|
17
|
-
* } catch (e) {
|
|
18
|
-
* if (e instanceof EnvError) {
|
|
19
|
-
* console.error(`Config issue [${e.code}]: ${e.message}`)
|
|
20
|
-
* // → "Config issue [MISSING_SUPABASE_URL]: SUPABASE_URL is required but not set"
|
|
21
|
-
* }
|
|
22
|
-
* }
|
|
23
|
-
* ```
|
|
24
|
-
*/
|
|
25
|
-
declare class EnvError extends Error {
|
|
26
|
-
/** Always `500` — environment errors are server-side issues. */
|
|
27
|
-
readonly status = 500;
|
|
28
|
-
/**
|
|
29
|
-
* Machine-readable error code.
|
|
30
|
-
*
|
|
31
|
-
* Known codes:
|
|
32
|
-
* - `"MISSING_SUPABASE_URL"` — `SUPABASE_URL` not set
|
|
33
|
-
* - `"MISSING_PUBLISHABLE_KEY"` — No publishable key found
|
|
34
|
-
* - `"MISSING_SECRET_KEY"` — No secret key found
|
|
35
|
-
* - `"ENV_ERROR"` — Generic environment error
|
|
36
|
-
*/
|
|
37
|
-
readonly code: string;
|
|
38
|
-
constructor(message: string, code?: string);
|
|
39
|
-
}
|
|
40
|
-
/**
|
|
41
|
-
* Thrown when authentication or authorization fails.
|
|
42
|
-
*
|
|
43
|
-
* Carries an HTTP `status` code suitable for returning directly in a response
|
|
44
|
-
* (typically `401` for invalid credentials, `500` for server-side auth failures).
|
|
45
|
-
*
|
|
46
|
-
* @example
|
|
47
|
-
* ```ts
|
|
48
|
-
* import { AuthError, createSupabaseContext } from '@supabase/server'
|
|
49
|
-
*
|
|
50
|
-
* const { data: ctx, error } = await createSupabaseContext(request, { allow: 'user' })
|
|
51
|
-
* if (error) {
|
|
52
|
-
* // error is an AuthError
|
|
53
|
-
* return Response.json(
|
|
54
|
-
* { error: error.message, code: error.code },
|
|
55
|
-
* { status: error.status },
|
|
56
|
-
* )
|
|
57
|
-
* }
|
|
58
|
-
* ```
|
|
59
|
-
*/
|
|
60
|
-
declare class AuthError extends Error {
|
|
61
|
-
/**
|
|
62
|
-
* HTTP status code.
|
|
63
|
-
*
|
|
64
|
-
* - `401` — Invalid or missing credentials
|
|
65
|
-
* - `500` — Server-side auth failure (e.g., missing JWKS, env misconfiguration)
|
|
66
|
-
*/
|
|
67
|
-
readonly status: number;
|
|
68
|
-
/**
|
|
69
|
-
* Machine-readable error code.
|
|
70
|
-
*
|
|
71
|
-
* Known codes:
|
|
72
|
-
* - `"INVALID_CREDENTIALS"` — No credential matched any allowed auth mode
|
|
73
|
-
* - `"CLIENT_ERROR"` — Failed to create a Supabase client after auth succeeded
|
|
74
|
-
* - `"AUTH_ERROR"` — Generic authentication error
|
|
75
|
-
* - Any `EnvError` code (propagated when env resolution fails during auth)
|
|
76
|
-
*/
|
|
77
|
-
readonly code: string;
|
|
78
|
-
constructor(message: string, code?: string, status?: number);
|
|
79
|
-
}
|
|
80
|
-
//#endregion
|
|
81
|
-
//#region src/core/resolve-env.d.ts
|
|
82
|
-
/**
|
|
83
|
-
* Resolves Supabase environment configuration from runtime environment variables.
|
|
84
|
-
*
|
|
85
|
-
* Reads `SUPABASE_URL`, keys (`SUPABASE_PUBLISHABLE_KEYS` / `SUPABASE_SECRET_KEYS`),
|
|
86
|
-
* and `SUPABASE_JWKS`. Works across Deno, Node.js, and Bun. For Cloudflare Workers,
|
|
87
|
-
* use `overrides` or enable node-compat.
|
|
88
|
-
*
|
|
89
|
-
* @param overrides - Partial values that take precedence over env vars.
|
|
90
|
-
* @returns `{ data: SupabaseEnv, error: null }` on success, `{ data: null, error: EnvError }` on failure.
|
|
91
|
-
*
|
|
92
|
-
* @example
|
|
93
|
-
* ```ts
|
|
94
|
-
* const { data: env, error } = resolveEnv()
|
|
95
|
-
* if (error) throw error
|
|
96
|
-
*
|
|
97
|
-
* // Override for tests
|
|
98
|
-
* const { data: env } = resolveEnv({ url: 'http://localhost:54321' })
|
|
99
|
-
* ```
|
|
100
|
-
*/
|
|
101
|
-
declare function resolveEnv(overrides?: Partial<SupabaseEnv>): {
|
|
102
|
-
data: SupabaseEnv;
|
|
103
|
-
error: null;
|
|
104
|
-
} | {
|
|
105
|
-
data: null;
|
|
106
|
-
error: EnvError;
|
|
107
|
-
};
|
|
108
|
-
//#endregion
|
|
109
|
-
//#region src/core/extract-credentials.d.ts
|
|
110
|
-
/**
|
|
111
|
-
* Extracts authentication credentials from an incoming HTTP request.
|
|
112
|
-
*
|
|
113
|
-
* Reads two headers:
|
|
114
|
-
* - `Authorization: Bearer <token>` → extracted as `token`
|
|
115
|
-
* - `apikey: <key>` → extracted as `apikey`
|
|
116
|
-
*
|
|
117
|
-
* This is a pure extraction step — no validation or verification is performed.
|
|
118
|
-
* Pass the result to {@link verifyCredentials} to validate against allowed auth modes.
|
|
119
|
-
*
|
|
120
|
-
* @param request - The incoming HTTP request.
|
|
121
|
-
* @returns The extracted {@link Credentials}. Fields are `null` when the corresponding header is absent.
|
|
122
|
-
*
|
|
123
|
-
* @example
|
|
124
|
-
* ```ts
|
|
125
|
-
* import { extractCredentials } from '@supabase/server/core'
|
|
126
|
-
*
|
|
127
|
-
* const creds = extractCredentials(request)
|
|
128
|
-
* console.log(creds.token) // "eyJhbGci..." or null
|
|
129
|
-
* console.log(creds.apikey) // "sb-abc123-publishable-..." or null
|
|
130
|
-
* ```
|
|
131
|
-
*/
|
|
132
|
-
declare function extractCredentials(request: Request): Credentials;
|
|
133
|
-
//#endregion
|
|
134
|
-
//#region src/core/verify-credentials.d.ts
|
|
135
|
-
/**
|
|
136
|
-
* Options for {@link verifyCredentials}.
|
|
137
|
-
*/
|
|
138
|
-
interface VerifyCredentialsOptions {
|
|
139
|
-
/**
|
|
140
|
-
* Auth mode(s) to try. Modes are attempted in order — the first match wins.
|
|
141
|
-
*
|
|
142
|
-
* @see {@link AllowWithKey} for the full syntax including named keys.
|
|
143
|
-
*/
|
|
144
|
-
allow: AllowWithKey | AllowWithKey[];
|
|
145
|
-
/** Optional environment overrides (passed through to {@link resolveEnv}). */
|
|
146
|
-
env?: Partial<SupabaseEnv>;
|
|
147
|
-
}
|
|
148
|
-
/**
|
|
149
|
-
* Verifies pre-extracted credentials against one or more allowed auth modes.
|
|
150
|
-
*
|
|
151
|
-
* Tries each mode in order — first match wins. Use {@link verifyAuth} to extract
|
|
152
|
-
* and verify in a single call.
|
|
153
|
-
*
|
|
154
|
-
* @param credentials - The credentials to verify (from {@link extractCredentials}).
|
|
155
|
-
* @param options - Allowed auth modes and optional env overrides.
|
|
156
|
-
* @returns `{ data: AuthResult, error: null }` on success, `{ data: null, error: AuthError }` on failure.
|
|
157
|
-
*
|
|
158
|
-
* @example
|
|
159
|
-
* ```ts
|
|
160
|
-
* const credentials = extractCredentials(request)
|
|
161
|
-
* const { data: auth, error } = await verifyCredentials(credentials, {
|
|
162
|
-
* allow: ['user', 'public'],
|
|
163
|
-
* })
|
|
164
|
-
* if (error) {
|
|
165
|
-
* return Response.json({ error: error.message }, { status: error.status })
|
|
166
|
-
* }
|
|
167
|
-
* ```
|
|
168
|
-
*/
|
|
169
|
-
declare function verifyCredentials(credentials: Credentials, options: VerifyCredentialsOptions): Promise<{
|
|
170
|
-
data: AuthResult;
|
|
171
|
-
error: null;
|
|
172
|
-
} | {
|
|
173
|
-
data: null;
|
|
174
|
-
error: AuthError;
|
|
175
|
-
}>;
|
|
176
|
-
//#endregion
|
|
177
|
-
//#region src/core/verify-auth.d.ts
|
|
178
|
-
/**
|
|
179
|
-
* Options for {@link verifyAuth}.
|
|
180
|
-
*/
|
|
181
|
-
interface VerifyAuthOptions {
|
|
182
|
-
/**
|
|
183
|
-
* Auth mode(s) to try. Modes are attempted in order — the first match wins.
|
|
184
|
-
*
|
|
185
|
-
* @see {@link AllowWithKey} for the full syntax including named keys.
|
|
186
|
-
*/
|
|
187
|
-
allow: AllowWithKey | AllowWithKey[];
|
|
188
|
-
/** Optional environment overrides (passed through to {@link resolveEnv}). */
|
|
189
|
-
env?: Partial<SupabaseEnv>;
|
|
190
|
-
}
|
|
191
|
-
/**
|
|
192
|
-
* Extracts credentials from a request and verifies them in a single step.
|
|
193
|
-
*
|
|
194
|
-
* This is a convenience function that combines {@link extractCredentials} and
|
|
195
|
-
* {@link verifyCredentials}. Use it when you want the full auth flow without
|
|
196
|
-
* needing to inspect the raw credentials.
|
|
197
|
-
*
|
|
198
|
-
* @param request - The incoming HTTP request.
|
|
199
|
-
* @param options - Auth modes to accept and optional environment overrides.
|
|
200
|
-
*
|
|
201
|
-
* @returns A result tuple: `{ data, error }`.
|
|
202
|
-
* - On success: `{ data: AuthResult, error: null }`
|
|
203
|
-
* - On failure: `{ data: null, error: AuthError }`
|
|
204
|
-
*
|
|
205
|
-
* @example
|
|
206
|
-
* ```ts
|
|
207
|
-
* import { verifyAuth } from '@supabase/server/core'
|
|
208
|
-
*
|
|
209
|
-
* const { data: auth, error } = await verifyAuth(request, {
|
|
210
|
-
* allow: 'user',
|
|
211
|
-
* })
|
|
212
|
-
*
|
|
213
|
-
* if (error) {
|
|
214
|
-
* return Response.json({ error: error.message }, { status: error.status })
|
|
215
|
-
* }
|
|
216
|
-
*
|
|
217
|
-
* console.log(auth.userClaims!.id) // "d0f1a2b3-..."
|
|
218
|
-
* ```
|
|
219
|
-
*/
|
|
220
|
-
declare function verifyAuth(request: Request, options: VerifyAuthOptions): Promise<{
|
|
221
|
-
data: AuthResult;
|
|
222
|
-
error: null;
|
|
223
|
-
} | {
|
|
224
|
-
data: null;
|
|
225
|
-
error: AuthError;
|
|
226
|
-
}>;
|
|
227
|
-
//#endregion
|
|
228
|
-
//#region src/core/create-context-client.d.ts
|
|
229
|
-
/**
|
|
230
|
-
* Creates a Supabase client scoped to the caller's context.
|
|
231
|
-
*
|
|
232
|
-
* Configured with a publishable key and (optionally) the caller's JWT,
|
|
233
|
-
* so Row-Level Security policies apply. Session persistence is disabled
|
|
234
|
-
* (stateless, one client per request).
|
|
235
|
-
*
|
|
236
|
-
* @param token - The caller's JWT, or `null` for anonymous access.
|
|
237
|
-
* @param env - Optional environment overrides (passed through to {@link resolveEnv}).
|
|
238
|
-
* @param keyName - Name of the publishable key to use. Falls back to `"default"`, then first available.
|
|
239
|
-
* @returns A configured {@link SupabaseClient} with RLS enforced.
|
|
240
|
-
* @throws {@link EnvError} If `SUPABASE_URL` is missing or the specified publishable key is not found.
|
|
241
|
-
*
|
|
242
|
-
* @example
|
|
243
|
-
* ```ts
|
|
244
|
-
* const { data: auth } = await verifyAuth(request, { allow: 'user' })
|
|
245
|
-
* const supabase = createContextClient(auth.token)
|
|
246
|
-
* const { data } = await supabase.rpc('get_my_items')
|
|
247
|
-
* ```
|
|
248
|
-
*/
|
|
249
|
-
declare function createContextClient<Database = unknown>(token?: string | null, env?: Partial<SupabaseEnv>, keyName?: string | null): SupabaseClient<Database>;
|
|
250
|
-
//#endregion
|
|
251
|
-
//#region src/core/create-admin-client.d.ts
|
|
252
|
-
/**
|
|
253
|
-
* Creates an admin Supabase client that bypasses Row-Level Security.
|
|
254
|
-
*
|
|
255
|
-
* Uses a secret key for authentication, giving full access to all data.
|
|
256
|
-
* Session persistence is disabled (stateless, one client per request).
|
|
257
|
-
*
|
|
258
|
-
* @param env - Optional environment overrides (passed through to {@link resolveEnv}).
|
|
259
|
-
* @param keyName - Name of the secret key to use. Falls back to `"default"`, then first available.
|
|
260
|
-
* @returns A configured {@link SupabaseClient} with admin (service-role) privileges.
|
|
261
|
-
* @throws {@link EnvError} If `SUPABASE_URL` is missing or the specified secret key is not found.
|
|
262
|
-
*
|
|
263
|
-
* @example
|
|
264
|
-
* ```ts
|
|
265
|
-
* const supabaseAdmin = createAdminClient()
|
|
266
|
-
* const { data } = await supabaseAdmin.from('audit_log').insert({ action: 'user_login' })
|
|
267
|
-
* ```
|
|
268
|
-
*/
|
|
269
|
-
declare function createAdminClient<Database = unknown>(env?: Partial<SupabaseEnv>, keyName?: string | null): SupabaseClient<Database>;
|
|
270
|
-
//#endregion
|
|
271
|
-
export { extractCredentials as a, EnvError as c, verifyCredentials as i, createContextClient as n, resolveEnv as o, verifyAuth as r, AuthError as s, createAdminClient as t };
|