@supatype/client 0.8.0 → 0.9.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.turbo/turbo-build.log +1 -1
- package/.turbo/turbo-test.log +73 -69
- package/.turbo/turbo-typecheck.log +1 -1
- package/CHANGELOG.md +110 -0
- package/bin/supatype-prepare.js +68 -0
- package/dist/auth.d.ts +50 -6
- package/dist/auth.d.ts.map +1 -1
- package/dist/auth.js +132 -40
- package/dist/auth.js.map +1 -1
- package/dist/bindings.d.ts +4 -1
- package/dist/bindings.d.ts.map +1 -1
- package/dist/bindings.js +2 -0
- package/dist/bindings.js.map +1 -1
- package/dist/error-codes-doc.d.ts +1 -1
- package/dist/error-codes-doc.d.ts.map +1 -1
- package/dist/error-codes-doc.js +7 -0
- package/dist/error-codes-doc.js.map +1 -1
- package/dist/errors.d.ts +81 -21
- package/dist/errors.d.ts.map +1 -1
- package/dist/errors.js +133 -46
- package/dist/errors.js.map +1 -1
- package/dist/graphql-transport.d.ts +10 -3
- package/dist/graphql-transport.d.ts.map +1 -1
- package/dist/graphql-transport.js +5 -5
- package/dist/graphql-transport.js.map +1 -1
- package/dist/index.d.ts +23 -16
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +66 -48
- package/dist/index.js.map +1 -1
- package/dist/latest-rows.d.ts +29 -0
- package/dist/latest-rows.d.ts.map +1 -0
- package/dist/latest-rows.js +51 -0
- package/dist/latest-rows.js.map +1 -0
- package/dist/mutation-request.d.ts +19 -5
- package/dist/mutation-request.d.ts.map +1 -1
- package/dist/mutation-request.js +18 -15
- package/dist/mutation-request.js.map +1 -1
- package/dist/page-env-script.d.ts +12 -0
- package/dist/page-env-script.d.ts.map +1 -0
- package/dist/page-env-script.js +30 -0
- package/dist/page-env-script.js.map +1 -0
- package/dist/preview.d.ts +2 -1
- package/dist/preview.d.ts.map +1 -1
- package/dist/preview.js +7 -3
- package/dist/preview.js.map +1 -1
- package/dist/query.d.ts +49 -19
- package/dist/query.d.ts.map +1 -1
- package/dist/query.js +102 -33
- package/dist/query.js.map +1 -1
- package/dist/realtime.d.ts +5 -2
- package/dist/realtime.d.ts.map +1 -1
- package/dist/realtime.js +13 -6
- package/dist/realtime.js.map +1 -1
- package/dist/runtime-env.d.ts +59 -0
- package/dist/runtime-env.d.ts.map +1 -0
- package/dist/runtime-env.js +167 -0
- package/dist/runtime-env.js.map +1 -0
- package/dist/setup-fault.d.ts +13 -0
- package/dist/setup-fault.d.ts.map +1 -0
- package/dist/setup-fault.js +17 -0
- package/dist/setup-fault.js.map +1 -0
- package/dist/storage.d.ts +10 -5
- package/dist/storage.d.ts.map +1 -1
- package/dist/storage.js +22 -13
- package/dist/storage.js.map +1 -1
- package/dist/table-subscription.d.ts +1 -1
- package/dist/table-subscription.d.ts.map +1 -1
- package/dist/table-subscription.js +3 -2
- package/dist/table-subscription.js.map +1 -1
- package/dist/thrown-error.d.ts +16 -0
- package/dist/thrown-error.d.ts.map +1 -0
- package/dist/thrown-error.js +31 -0
- package/dist/thrown-error.js.map +1 -0
- package/dist/types.d.ts +59 -15
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js.map +1 -1
- package/package.json +6 -2
- package/src/auth.ts +135 -44
- package/src/bindings.ts +4 -1
- package/src/error-codes-doc.ts +9 -1
- package/src/errors.ts +155 -57
- package/src/graphql-transport.ts +7 -8
- package/src/index.ts +82 -60
- package/src/latest-rows.ts +78 -0
- package/src/mutation-request.ts +34 -19
- package/src/page-env-script.ts +27 -0
- package/src/preview.ts +8 -4
- package/src/query.ts +128 -52
- package/src/realtime.ts +15 -6
- package/src/runtime-env.ts +220 -0
- package/src/setup-fault.ts +18 -0
- package/src/storage.ts +26 -16
- package/src/table-subscription.ts +3 -3
- package/src/thrown-error.ts +31 -0
- package/src/types.ts +65 -14
- package/tests/auth-errors.test.ts +3 -3
- package/tests/auth-events.test.ts +3 -7
- package/tests/auth-sso.test.ts +260 -0
- package/tests/auth-url-session.test.ts +6 -25
- package/tests/data-plane-auth.test.ts +1 -6
- package/tests/draft-insert.test.ts +9 -9
- package/tests/draft-update.test.ts +5 -5
- package/tests/entry-points.test.ts +9 -0
- package/tests/error-family.test.ts +79 -0
- package/tests/helpers/auth-unmount-binding.ts +63 -0
- package/tests/helpers/oauth-binding.ts +4 -3
- package/tests/helpers/stale-read-binding.ts +113 -0
- package/tests/helpers/thrown-error-binding.ts +127 -0
- package/tests/minimal-writes.test.ts +118 -0
- package/tests/postgrest-error-codes.test.ts +5 -5
- package/tests/query.test.ts +3 -3
- package/tests/rate-limit-integration.test.ts +1 -1
- package/tests/realtime-session-auth.test.ts +1 -4
- package/tests/result-error-class.test.ts +107 -0
- package/tests/retry-policy.test.ts +1 -1
- package/tests/rpc-resend.test.ts +33 -8
- package/tests/runtime-env-bundle.test.ts +121 -0
- package/tests/runtime-env-page-script.test.ts +174 -0
- package/tests/runtime-env.test.ts +224 -0
- package/tests/server-bodies.test.ts +3 -3
- package/tests/supatype-prepare.test.ts +139 -0
- package/tests/support/browser.ts +28 -0
- package/tests/support/jwt.ts +9 -0
- package/tests/thrown-error.test.ts +59 -0
- package/tests/unauthorized-resend.test.ts +107 -0
- package/tsconfig.tsbuildinfo +1 -1
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"thrown-error.js","sourceRoot":"","sources":["../src/thrown-error.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,aAAa,CAAA;AAE3C;;;;;;;;;;;;GAYG;AACH,MAAM,UAAU,YAAY,CAAC,KAAa,EAAE,MAAe;IACzD,OAAO,CAAC,KAAK,CAAC,cAAc,KAAK,sBAAsB,EAAE,MAAM,CAAC,CAAA;IAChE,OAAO,aAAa,CAAC,MAAM,CAAC,CAAA;AAC9B,CAAC;AAED;;;;;GAKG;AACH,SAAS,aAAa,CAAC,MAAe;IACpC,IAAI,MAAM,YAAY,aAAa;QAAE,OAAO,MAAM,CAAA;IAClD,MAAM,OAAO,GAAG,MAAM,YAAY,KAAK,CAAC,CAAC,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC,CAAC,OAAO,MAAM,KAAK,QAAQ,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,eAAe,CAAA;IAChH,OAAO,IAAI,aAAa,CAAC,EAAE,OAAO,EAAE,EAAE,EAAE,KAAK,EAAE,MAAM,EAAE,CAAC,CAAA;AAC1D,CAAC"}
|
package/dist/types.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { TableRelationship } from "./select-types.js";
|
|
2
|
+
import type { SupatypeError } from "./errors.js";
|
|
2
3
|
export interface UserIdentity {
|
|
3
4
|
id: string;
|
|
4
5
|
userId: string;
|
|
@@ -49,6 +50,45 @@ export type AuthChangeEvent = "SIGNED_IN" | "SIGNED_OUT" | "TOKEN_REFRESHED" | "
|
|
|
49
50
|
* React Native helpers should pass `pkce` (recommended default for mobile).
|
|
50
51
|
*/
|
|
51
52
|
export type AuthFlowType = "pkce" | "implicit";
|
|
53
|
+
/** How `signInWithSSO` starts and ends. */
|
|
54
|
+
export interface SignInWithSSOOptions {
|
|
55
|
+
/** Where the browser lands once signed in; honoured on the site URL's host or the redirect allow list. */
|
|
56
|
+
redirectTo?: string | undefined;
|
|
57
|
+
/** The CAPTCHA token, when the project requires one. */
|
|
58
|
+
captchaToken?: string | undefined;
|
|
59
|
+
/** Return the identity provider's URL and leave the browser where it is. */
|
|
60
|
+
skipBrowserRedirect?: boolean | undefined;
|
|
61
|
+
/**
|
|
62
|
+
* Default: `pkce`, so the session comes back as a `?code=` only this browser can exchange, never
|
|
63
|
+
* as tokens in the URL. `implicit` returns tokens in the fragment. (`signInWithOAuth` defaults to
|
|
64
|
+
* `implicit`.) A sign-in the identity provider starts always comes back implicit, and the client
|
|
65
|
+
* accepts it either way.
|
|
66
|
+
*/
|
|
67
|
+
flowType?: AuthFlowType | undefined;
|
|
68
|
+
}
|
|
69
|
+
/** Sign in through the SAML identity provider registered for the user's email domain. */
|
|
70
|
+
export interface SignInWithSSODomain {
|
|
71
|
+
/** The domain of the user's email address: `acme.com`. */
|
|
72
|
+
domain: string;
|
|
73
|
+
providerId?: never;
|
|
74
|
+
options?: SignInWithSSOOptions | undefined;
|
|
75
|
+
}
|
|
76
|
+
/** Sign in through one SAML identity provider, named by its ID. */
|
|
77
|
+
export interface SignInWithSSOProvider {
|
|
78
|
+
/** The provider's UUID, as the admin API reports it. */
|
|
79
|
+
providerId: string;
|
|
80
|
+
domain?: never;
|
|
81
|
+
options?: SignInWithSSOOptions | undefined;
|
|
82
|
+
}
|
|
83
|
+
/** What `signInWithSSO` takes: an email domain or a provider's ID, never both. */
|
|
84
|
+
export type SignInWithSSOCredentials = SignInWithSSODomain | SignInWithSSOProvider;
|
|
85
|
+
/** What `signInWithSSO` answers: the identity provider's URL, or why there is none. */
|
|
86
|
+
export interface SignInWithSSOResult {
|
|
87
|
+
data: {
|
|
88
|
+
url: string;
|
|
89
|
+
};
|
|
90
|
+
error: SupatypeError | null;
|
|
91
|
+
}
|
|
52
92
|
export interface AuthMFAEnrollResponse {
|
|
53
93
|
/** The MFA factor ID */
|
|
54
94
|
id: string;
|
|
@@ -94,7 +134,10 @@ export interface AuthMFAListFactorsResponse {
|
|
|
94
134
|
phone: Factor[];
|
|
95
135
|
}
|
|
96
136
|
export type OtpType = "signup" | "magiclink" | "recovery" | "invite" | "email_change" | "sms" | "phone_change" | "email";
|
|
97
|
-
|
|
137
|
+
/** The class every result error is; its fields are `SupatypeErrorFields`. */
|
|
138
|
+
export type { SupatypeError };
|
|
139
|
+
/** The fields a `SupatypeError` carries, as given to its constructor and as its JSON. */
|
|
140
|
+
export interface SupatypeErrorFields {
|
|
98
141
|
message: string;
|
|
99
142
|
status?: number | undefined;
|
|
100
143
|
code?: string | undefined;
|
|
@@ -120,15 +163,8 @@ export interface SupatypeError {
|
|
|
120
163
|
*/
|
|
121
164
|
details?: string | undefined;
|
|
122
165
|
}
|
|
123
|
-
/**
|
|
124
|
-
|
|
125
|
-
*
|
|
126
|
-
* `provider` names the OAuth provider this browser started the sign-in with, when the client
|
|
127
|
-
* remembers one, so an app showing one button per provider can put the message by the right one.
|
|
128
|
-
*/
|
|
129
|
-
export interface AuthRedirectError extends SupatypeError {
|
|
130
|
-
provider?: string | undefined;
|
|
131
|
-
}
|
|
166
|
+
/** A sign-in refused in the redirect URL: the class, with the provider it was started with. */
|
|
167
|
+
export type { AuthRedirectError } from "./errors.js";
|
|
132
168
|
/**
|
|
133
169
|
* A column in the response that carries a read restriction.
|
|
134
170
|
*
|
|
@@ -340,10 +376,19 @@ export interface SelectQueryOptions {
|
|
|
340
376
|
profile?: string | undefined;
|
|
341
377
|
}
|
|
342
378
|
export interface SupatypeClientConfig {
|
|
343
|
-
/**
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
379
|
+
/**
|
|
380
|
+
* Base URL of the Supatype gateway (e.g. http://localhost:18473). Left out, or empty as a bundler
|
|
381
|
+
* leaves an unset name, it is read from `window.__SUPATYPE_ENV__` (set by
|
|
382
|
+
* `<script src="/supatype-env.js">`) when the first request is made. It is never guessed from the
|
|
383
|
+
* page's own address: with neither, that request throws `MissingClientValuesError`.
|
|
384
|
+
*/
|
|
385
|
+
url?: string | undefined;
|
|
386
|
+
/**
|
|
387
|
+
* Anon JWT key. Left out, or empty, it is read from `window.__SUPATYPE_ENV__` when the first
|
|
388
|
+
* request is made; with neither, that request throws `MissingClientValuesError` naming the
|
|
389
|
+
* variables to set. `createClient` itself never throws for either.
|
|
390
|
+
*/
|
|
391
|
+
anonKey?: string | undefined;
|
|
347
392
|
/**
|
|
348
393
|
* Service role key: bypasses row-level security.
|
|
349
394
|
* Only set this in trusted server-side or developer-tool contexts.
|
|
@@ -439,5 +484,4 @@ export interface SupatypeClientConfig {
|
|
|
439
484
|
*/
|
|
440
485
|
headers?: Record<string, string> | (() => Record<string, string>) | undefined;
|
|
441
486
|
}
|
|
442
|
-
export {};
|
|
443
487
|
//# sourceMappingURL=types.d.ts.map
|
package/dist/types.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,mBAAmB,CAAA;
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,mBAAmB,CAAA;AAC1D,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,aAAa,CAAA;AAIhD,MAAM,WAAW,YAAY;IAC3B,EAAE,EAAE,MAAM,CAAA;IACV,MAAM,EAAE,MAAM,CAAA;IACd,YAAY,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAA;IACrC,UAAU,EAAE,MAAM,CAAA;IAClB,QAAQ,EAAE,MAAM,CAAA;IAChB,SAAS,EAAE,MAAM,CAAA;IACjB,SAAS,EAAE,MAAM,CAAA;IACjB,YAAY,CAAC,EAAE,MAAM,GAAG,SAAS,CAAA;CAClC;AAED,MAAM,WAAW,MAAM;IACrB,EAAE,EAAE,MAAM,CAAA;IACV,YAAY,CAAC,EAAE,MAAM,GAAG,SAAS,CAAA;IACjC,UAAU,EAAE,MAAM,GAAG,OAAO,GAAG,UAAU,CAAA;IACzC,MAAM,EAAE,UAAU,GAAG,YAAY,CAAA;IACjC,SAAS,EAAE,MAAM,CAAA;IACjB,SAAS,EAAE,MAAM,CAAA;IACjB,KAAK,CAAC,EAAE,MAAM,GAAG,SAAS,CAAA;CAC3B;AAED,MAAM,WAAW,IAAI;IACnB,EAAE,EAAE,MAAM,CAAA;IACV,KAAK,CAAC,EAAE,MAAM,GAAG,SAAS,CAAA;IAC1B,KAAK,CAAC,EAAE,MAAM,GAAG,SAAS,CAAA;IAC1B,IAAI,CAAC,EAAE,MAAM,GAAG,SAAS,CAAA;IACzB,WAAW,CAAC,EAAE,OAAO,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;;;;WAIG;QACH,YAAY,CAAC,EAAE,MAAM,GAAG,SAAS,CAAA;QACjC;;;WAGG;QACH,OAAO,CAAC,EAAE,WAAW,GAAG,SAAS,CAAA;QACjC;;;;;WAKG;QACH,kBAAkB,CAAC,EAAE,OAAO,GAAG,SAAS,CAAA;KACzC,GAAG,SAAS,CAAA;IACb;;;;;;;;OAQG;IACH,KAAK,CAAC,EAAE,OAAO,GAAG,SAAS,CAAA;IAC3B;;;;;;OAMG;IACH,OAAO,CAAC,EAAE,MAAM,GAAG,SAAS,CAAA;IAC5B;;OAEG;IACH,UAAU,CAAC,EAAE,OAAO,kBAAkB,EAAE,UAAU,GAAG,SAAS,CAAA;IAC9D;;;;OAIG;IACH,cAAc,CAAC,EAAE,OAAO,GAAG,SAAS,CAAA;IACpC;;;;;;;;;;;;;;;OAeG;IACH,WAAW,CAAC,EAAE,MAAM,GAAG,SAAS,CAAA;IAChC;;;OAGG;IACH,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,GAAG,CAAC,MAAM,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC,GAAG,SAAS,CAAA;CAC9E"}
|
package/dist/types.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AA2aA;;;;;;;GAOG;AACH,MAAM,CAAC,MAAM,YAAY,GAAG,OAAO,CAAA;AAEnC;;;;GAIG;AACH,MAAM,CAAC,MAAM,iBAAiB,GAAG,UAAU,CAAA"}
|
package/package.json
CHANGED
|
@@ -1,11 +1,14 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@supatype/client",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.9.1",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"description": "Supatype client SDK: typed REST, auth, storage, realtime",
|
|
6
6
|
"type": "module",
|
|
7
7
|
"main": "./dist/index.js",
|
|
8
8
|
"types": "./dist/index.d.ts",
|
|
9
|
+
"bin": {
|
|
10
|
+
"supatype-prepare": "./bin/supatype-prepare.js"
|
|
11
|
+
},
|
|
9
12
|
"exports": {
|
|
10
13
|
".": {
|
|
11
14
|
"import": "./dist/index.js",
|
|
@@ -19,6 +22,7 @@
|
|
|
19
22
|
"devDependencies": {
|
|
20
23
|
"@types/node": "^22",
|
|
21
24
|
"typescript": "^5",
|
|
25
|
+
"vite": "^6",
|
|
22
26
|
"vitest": "^4.0.18"
|
|
23
27
|
},
|
|
24
28
|
"dependencies": {
|
|
@@ -28,7 +32,7 @@
|
|
|
28
32
|
"build": "tsc --project tsconfig.json",
|
|
29
33
|
"dev": "tsc --project tsconfig.json --watch",
|
|
30
34
|
"typecheck": "tsc --project tests/tsconfig.json --noEmit",
|
|
31
|
-
"test": "vitest run",
|
|
35
|
+
"test": "vitest run --maxWorkers=50%",
|
|
32
36
|
"clean": "rm -rf dist *.tsbuildinfo"
|
|
33
37
|
}
|
|
34
38
|
}
|
package/src/auth.ts
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import type {
|
|
2
2
|
AuthChangeEvent,
|
|
3
3
|
AuthFlowType,
|
|
4
|
-
AuthRedirectError,
|
|
5
4
|
AuthMFAChallengeResponse,
|
|
6
5
|
AuthMFAEnrollResponse,
|
|
7
6
|
AuthMFAListFactorsResponse,
|
|
@@ -9,7 +8,9 @@ import type {
|
|
|
9
8
|
Factor,
|
|
10
9
|
OtpType,
|
|
11
10
|
Session,
|
|
12
|
-
|
|
11
|
+
SignInWithSSOCredentials,
|
|
12
|
+
SignInWithSSOOptions,
|
|
13
|
+
SignInWithSSOResult,
|
|
13
14
|
User,
|
|
14
15
|
UserIdentity,
|
|
15
16
|
} from "./types.js"
|
|
@@ -38,6 +39,9 @@ import {
|
|
|
38
39
|
type Guard,
|
|
39
40
|
} from "./json.js"
|
|
40
41
|
import { decodeJwtPayload } from "./jwt.js"
|
|
42
|
+
import { rethrowSetupFault } from "./setup-fault.js"
|
|
43
|
+
import { settle, type Deferred } from "./runtime-env.js"
|
|
44
|
+
import { AuthRedirectError, SupatypeError } from "./errors.js"
|
|
41
45
|
|
|
42
46
|
export type AuthListener = (event: AuthChangeEvent, session: Session | null) => void
|
|
43
47
|
|
|
@@ -63,6 +67,11 @@ const DEFAULT_COOKIE_PREFIX = "st"
|
|
|
63
67
|
/** Storage key suffixes for the state an OAuth redirect has to survive. */
|
|
64
68
|
const VERIFIER_SUFFIX = "-code-verifier"
|
|
65
69
|
const PROVIDER_SUFFIX = "-oauth-provider"
|
|
70
|
+
/**
|
|
71
|
+
* What a SAML sign-in is remembered as: the `provider` of an `AuthRedirectError` the auth server
|
|
72
|
+
* sent back in the URL for a sign-in `signInWithSSO` started.
|
|
73
|
+
*/
|
|
74
|
+
export const SSO_PROVIDER = "sso"
|
|
66
75
|
const DEBUG_AUTH = typeof process !== "undefined" && process.env["NEXT_PUBLIC_SUPATYPE_DEBUG_AUTH"] === "1"
|
|
67
76
|
|
|
68
77
|
/**
|
|
@@ -82,7 +91,7 @@ function authErrorFromBody(body: Record<string, unknown>, status: number): Supat
|
|
|
82
91
|
const message =
|
|
83
92
|
text("msg") ?? text("message") ?? text("error_description") ?? text("error") ??
|
|
84
93
|
`Auth request failed (HTTP ${status})`
|
|
85
|
-
return { message, status, ...(code !== undefined && { code }) }
|
|
94
|
+
return new SupatypeError({ message, status, ...(code !== undefined && { code }) })
|
|
86
95
|
}
|
|
87
96
|
|
|
88
97
|
/** The assurance levels the client reports; the server's unused `aal3` reads as unknown. */
|
|
@@ -135,8 +144,8 @@ function snakeCaseUser(user: Record<string, unknown>): Record<string, unknown> {
|
|
|
135
144
|
}
|
|
136
145
|
|
|
137
146
|
export class AuthClient {
|
|
138
|
-
private readonly
|
|
139
|
-
private readonly
|
|
147
|
+
private readonly urlSource: Deferred<string>
|
|
148
|
+
private readonly baseHeadersSource: Deferred<Record<string, string>>
|
|
140
149
|
private currentSession: Session | null = null
|
|
141
150
|
private readonly listeners = new Map<string, AuthListener>()
|
|
142
151
|
private listenerIdCounter = 0
|
|
@@ -163,9 +172,13 @@ export class AuthClient {
|
|
|
163
172
|
/** In-memory PKCE code_verifier for the in-flight OAuth / OTP redirect. */
|
|
164
173
|
private codeVerifier: string | null = null
|
|
165
174
|
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
175
|
+
/**
|
|
176
|
+
* `url` and `baseHeaders` may be functions, read for each request. `createClient` passes
|
|
177
|
+
* functions, so a client made before the page has said where the API is fails only at a request.
|
|
178
|
+
*/
|
|
179
|
+
constructor(url: Deferred<string>, baseHeaders: Deferred<Record<string, string>>, opts: AuthClientOptions = {}) {
|
|
180
|
+
this.urlSource = url
|
|
181
|
+
this.baseHeadersSource = baseHeaders
|
|
169
182
|
this.persistSession = opts.persistSession ?? true
|
|
170
183
|
this.autoRefreshToken = opts.autoRefreshToken ?? true
|
|
171
184
|
this.refreshExpiredToken = opts.refreshExpiredToken ?? true
|
|
@@ -189,6 +202,16 @@ export class AuthClient {
|
|
|
189
202
|
}
|
|
190
203
|
}
|
|
191
204
|
|
|
205
|
+
/** The auth address, read now. */
|
|
206
|
+
private get url(): string {
|
|
207
|
+
return settle(this.urlSource)
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
/** The anon headers every auth request starts from, read now. */
|
|
211
|
+
private get baseHeaders(): Record<string, string> {
|
|
212
|
+
return settle(this.baseHeadersSource)
|
|
213
|
+
}
|
|
214
|
+
|
|
192
215
|
/** The session the client starts with: the one passed in, else the persisted one. */
|
|
193
216
|
private loadInitialSession(opts: AuthClientOptions): Promise<void> {
|
|
194
217
|
if (opts.initialSession !== undefined) {
|
|
@@ -242,12 +265,12 @@ export class AuthClient {
|
|
|
242
265
|
const provider = await this.takeFlowItem(PROVIDER_SUFFIX)
|
|
243
266
|
this.currentSession = null
|
|
244
267
|
const { error } = await this.getSessionFromUrl(href).catch((err: unknown) => ({
|
|
245
|
-
error: { message: err instanceof Error ? err.message : "Could not finish the sign-in" },
|
|
268
|
+
error: new SupatypeError({ message: err instanceof Error ? err.message : "Could not finish the sign-in" }, { cause: err }),
|
|
246
269
|
}))
|
|
247
270
|
cleanBrowserUrl(href)
|
|
248
271
|
if (error === null) return { error: null }
|
|
249
272
|
this._setSession(null)
|
|
250
|
-
return { error: { ...error, ...(provider !== null && { provider }) } }
|
|
273
|
+
return { error: new AuthRedirectError({ ...error.toJSON(), ...(provider !== null && { provider }) }, { cause: error }) }
|
|
251
274
|
}
|
|
252
275
|
|
|
253
276
|
/** A URL with tokens or a refusal, or with a `code` for a PKCE flow this client started. */
|
|
@@ -319,15 +342,36 @@ export class AuthClient {
|
|
|
319
342
|
this._setSession(null)
|
|
320
343
|
return
|
|
321
344
|
}
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
345
|
+
await this.refreshOnce()
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
/**
|
|
349
|
+
* Refresh after the server refused `refusedToken` as expired or invalid, whatever the local clock
|
|
350
|
+
* says of it. True when the session now holds a different token, so a request refused with that
|
|
351
|
+
* one is worth sending again; false when signed out, when the refresh failed or handed back the
|
|
352
|
+
* same token, or when this client leaves expired sessions for the server to refuse.
|
|
353
|
+
*/
|
|
354
|
+
async refreshRefusedSession(refusedToken: string): Promise<boolean> {
|
|
355
|
+
await this.ready
|
|
356
|
+
const session = this.currentSession
|
|
357
|
+
if (session === null) return false
|
|
358
|
+
// Another request's refresh has already replaced it.
|
|
359
|
+
if (session.accessToken !== refusedToken) return true
|
|
360
|
+
if (!this.refreshExpiredToken) return false
|
|
361
|
+
await this.refreshOnce()
|
|
362
|
+
const token = this.currentAccessToken
|
|
363
|
+
return token !== null && token !== refusedToken
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
/** One refresh at a time: a second caller waits for the one already in flight. */
|
|
367
|
+
private async refreshOnce(): Promise<void> {
|
|
368
|
+
if (this.refreshInFlight === null) {
|
|
369
|
+
this.refreshInFlight = this.refreshSession()
|
|
370
|
+
.then(() => undefined)
|
|
371
|
+
.finally(() => {
|
|
372
|
+
this.refreshInFlight = null
|
|
373
|
+
})
|
|
325
374
|
}
|
|
326
|
-
this.refreshInFlight = this.refreshSession()
|
|
327
|
-
.then(() => undefined)
|
|
328
|
-
.finally(() => {
|
|
329
|
-
this.refreshInFlight = null
|
|
330
|
-
})
|
|
331
375
|
await this.refreshInFlight
|
|
332
376
|
}
|
|
333
377
|
|
|
@@ -453,7 +497,7 @@ export class AuthClient {
|
|
|
453
497
|
// navigating there shows as raw text; asking first lets the caller show a message instead.
|
|
454
498
|
if (await this.providerDisabled(opts.provider)) {
|
|
455
499
|
const message = `Sign-in with ${opts.provider} is not enabled for this project.`
|
|
456
|
-
return { data: { url: "", provider: opts.provider }, error: { message, status: 400, code: "provider_disabled" } }
|
|
500
|
+
return { data: { url: "", provider: opts.provider }, error: new SupatypeError({ message, status: 400, code: "provider_disabled" }) }
|
|
457
501
|
}
|
|
458
502
|
await this.writeFlowItem(PROVIDER_SUFFIX, opts.provider)
|
|
459
503
|
const url = this.endpoint("/authorize", opts.options?.redirectTo)
|
|
@@ -466,13 +510,53 @@ export class AuthClient {
|
|
|
466
510
|
}
|
|
467
511
|
const flowType = opts.options?.flowType ?? "implicit"
|
|
468
512
|
if (flowType === "pkce") {
|
|
469
|
-
await this.beginPkce()
|
|
470
|
-
url.searchParams.set("code_challenge", createCodeChallengeS256(this.codeVerifier!))
|
|
471
|
-
url.searchParams.set("code_challenge_method", PKCE_METHOD_S256)
|
|
513
|
+
for (const [key, value] of Object.entries(await this.beginPkce())) url.searchParams.set(key, value)
|
|
472
514
|
}
|
|
473
515
|
return { data: { url: url.toString(), provider: opts.provider }, error: null }
|
|
474
516
|
}
|
|
475
517
|
|
|
518
|
+
/**
|
|
519
|
+
* Sign in with SAML single sign-on, through the identity provider registered for the user's email
|
|
520
|
+
* domain, or one named by its ID.
|
|
521
|
+
*
|
|
522
|
+
* Asks `POST /sso` for the identity provider's URL and sends the browser there, unless
|
|
523
|
+
* `skipBrowserRedirect` is set or there is no browser. The identity provider returns the user to
|
|
524
|
+
* the auth server, which sends them on to `redirectTo`; the client finishes the sign-in on start,
|
|
525
|
+
* as it does for OAuth. A refusal (no provider for the domain, a disabled one) is returned and the
|
|
526
|
+
* page stays put.
|
|
527
|
+
*
|
|
528
|
+
* PKCE by default (`flowType`), unlike `signInWithOAuth`: the sign-in comes back as a `?code=`
|
|
529
|
+
* this browser exchanges with its stored verifier.
|
|
530
|
+
*/
|
|
531
|
+
async signInWithSSO(params: SignInWithSSOCredentials): Promise<SignInWithSSOResult> {
|
|
532
|
+
const res = await this.send(`${this.url}/sso`, {
|
|
533
|
+
method: "POST",
|
|
534
|
+
headers: this.baseHeaders,
|
|
535
|
+
body: JSON.stringify(await this.ssoRequestBody(params)),
|
|
536
|
+
})
|
|
537
|
+
if (!res.ok) return { data: { url: "" }, error: await this._parseError(res) }
|
|
538
|
+
const url = (await readJsonObject(res))["url"]
|
|
539
|
+
if (typeof url !== "string" || url === "") {
|
|
540
|
+
const message = "The auth server answered /sso without a sign-in URL"
|
|
541
|
+
return { data: { url: "" }, error: new SupatypeError({ message, status: res.status }) }
|
|
542
|
+
}
|
|
543
|
+
await this.writeFlowItem(PROVIDER_SUFFIX, SSO_PROVIDER)
|
|
544
|
+
if (params.options?.skipBrowserRedirect !== true && typeof window !== "undefined") window.location.assign(url)
|
|
545
|
+
return { data: { url }, error: null }
|
|
546
|
+
}
|
|
547
|
+
|
|
548
|
+
/** The `/sso` body: the domain or the provider's ID, the caller's options, and any PKCE challenge. */
|
|
549
|
+
private async ssoRequestBody(params: SignInWithSSOCredentials): Promise<Record<string, unknown>> {
|
|
550
|
+
const opts: SignInWithSSOOptions = params.options ?? {}
|
|
551
|
+
const body: Record<string, unknown> = params.domain !== undefined ? { domain: params.domain } : { provider_id: params.providerId }
|
|
552
|
+
// A fetch cannot follow the server's 303 to another origin, so ask for the URL as JSON.
|
|
553
|
+
body["skip_http_redirect"] = true
|
|
554
|
+
if (opts.redirectTo !== undefined) body["redirect_to"] = opts.redirectTo
|
|
555
|
+
if (opts.captchaToken !== undefined) body["supatype_meta_security"] = { captcha_token: opts.captchaToken }
|
|
556
|
+
const flowType = opts.flowType ?? "pkce"
|
|
557
|
+
return flowType === "pkce" ? { ...body, ...(await this.beginPkce()) } : body
|
|
558
|
+
}
|
|
559
|
+
|
|
476
560
|
/**
|
|
477
561
|
* True only when the server's `/settings` list the provider as off. A provider they do not list
|
|
478
562
|
* (a `custom:` one) or settings that cannot be read leave the decision to `/authorize`. Looked up
|
|
@@ -531,8 +615,8 @@ export class AuthClient {
|
|
|
531
615
|
|
|
532
616
|
/**
|
|
533
617
|
* Exchange a PKCE auth code for a session (`POST /token?grant_type=pkce`).
|
|
534
|
-
* Requires a prior
|
|
535
|
-
* so the matching `code_verifier` is available in memory / storage.
|
|
618
|
+
* Requires a prior PKCE sign-in in this browser (`signInWithSSO`, or `signInWithOAuth` or OTP with
|
|
619
|
+
* `flowType: "pkce"`), so the matching `code_verifier` is available in memory / storage.
|
|
536
620
|
*/
|
|
537
621
|
async exchangeCodeForSession(authCode: string): Promise<{
|
|
538
622
|
data: { session: Session | null; user: User | null }
|
|
@@ -542,10 +626,10 @@ export class AuthClient {
|
|
|
542
626
|
if (verifier === null) {
|
|
543
627
|
return {
|
|
544
628
|
data: { session: null, user: null },
|
|
545
|
-
error: {
|
|
546
|
-
message: "Missing PKCE code_verifier. Start OAuth with flowType: \"pkce\" first.",
|
|
629
|
+
error: new SupatypeError({
|
|
630
|
+
message: "Missing PKCE code_verifier. Start the sign-in (SSO, or OAuth with flowType: \"pkce\") in this browser first.",
|
|
547
631
|
status: 400,
|
|
548
|
-
},
|
|
632
|
+
}),
|
|
549
633
|
}
|
|
550
634
|
}
|
|
551
635
|
const res = await this.send(`${this.url}/token?grant_type=pkce`, {
|
|
@@ -572,7 +656,7 @@ export class AuthClient {
|
|
|
572
656
|
if (params === null) {
|
|
573
657
|
return {
|
|
574
658
|
data: { session: null, user: null },
|
|
575
|
-
error: { message: `Invalid redirect URL: ${url}`, status: 400 },
|
|
659
|
+
error: new SupatypeError({ message: `Invalid redirect URL: ${url}`, status: 400 }),
|
|
576
660
|
}
|
|
577
661
|
}
|
|
578
662
|
const { query, fragment: hashParams } = params
|
|
@@ -612,10 +696,10 @@ export class AuthClient {
|
|
|
612
696
|
|
|
613
697
|
return {
|
|
614
698
|
data: { session: null, user: null },
|
|
615
|
-
error: {
|
|
699
|
+
error: new SupatypeError({
|
|
616
700
|
message: "No auth code or tokens found in redirect URL",
|
|
617
701
|
status: 400,
|
|
618
|
-
},
|
|
702
|
+
}),
|
|
619
703
|
}
|
|
620
704
|
}
|
|
621
705
|
|
|
@@ -720,11 +804,11 @@ export class AuthClient {
|
|
|
720
804
|
error: SupatypeError | null
|
|
721
805
|
}> {
|
|
722
806
|
if (this.currentSession === null) {
|
|
723
|
-
return { data: { session: null }, error: { message: "No active session" } }
|
|
807
|
+
return { data: { session: null }, error: new SupatypeError({ message: "No active session" }) }
|
|
724
808
|
}
|
|
725
809
|
if (!this.currentSession.refreshToken?.trim()) {
|
|
726
810
|
this._setSession(null)
|
|
727
|
-
return { data: { session: null }, error: { message: "No refresh token" } }
|
|
811
|
+
return { data: { session: null }, error: new SupatypeError({ message: "No refresh token" }) }
|
|
728
812
|
}
|
|
729
813
|
try {
|
|
730
814
|
const res = await this.send(`${this.url}/token?grant_type=refresh_token`, {
|
|
@@ -739,9 +823,11 @@ export class AuthClient {
|
|
|
739
823
|
}
|
|
740
824
|
return await this._parseAuthResponse(res)
|
|
741
825
|
} catch (err) {
|
|
826
|
+
// Not a refused refresh: nothing was sent, and the session may be perfectly good.
|
|
827
|
+
rethrowSetupFault(err)
|
|
742
828
|
const message = err instanceof Error ? err.message : "Refresh failed"
|
|
743
829
|
this._setSession(null)
|
|
744
|
-
return { data: { session: null }, error: { message } }
|
|
830
|
+
return { data: { session: null }, error: new SupatypeError({ message }, { cause: err }) }
|
|
745
831
|
}
|
|
746
832
|
}
|
|
747
833
|
|
|
@@ -764,7 +850,7 @@ export class AuthClient {
|
|
|
764
850
|
data?: Record<string, unknown> | undefined
|
|
765
851
|
}): Promise<{ data: { user: User | null }; error: SupatypeError | null }> {
|
|
766
852
|
if (this.currentSession === null) {
|
|
767
|
-
return { data: { user: null }, error: { message: "Not authenticated" } }
|
|
853
|
+
return { data: { user: null }, error: new SupatypeError({ message: "Not authenticated" }) }
|
|
768
854
|
}
|
|
769
855
|
const res = await this.send(`${this.url}/user`, {
|
|
770
856
|
method: "PUT",
|
|
@@ -905,7 +991,7 @@ export class AuthClient {
|
|
|
905
991
|
error: SupatypeError | null
|
|
906
992
|
}> => {
|
|
907
993
|
if (this.currentSession === null) {
|
|
908
|
-
return { data: null, error: { message: "Not authenticated" } }
|
|
994
|
+
return { data: null, error: new SupatypeError({ message: "Not authenticated" }) }
|
|
909
995
|
}
|
|
910
996
|
const body: Record<string, unknown> = {
|
|
911
997
|
factor_type: params.factorType,
|
|
@@ -954,7 +1040,7 @@ export class AuthClient {
|
|
|
954
1040
|
error: SupatypeError | null
|
|
955
1041
|
}> => {
|
|
956
1042
|
if (this.currentSession === null) {
|
|
957
|
-
return { data: null, error: { message: "Not authenticated" } }
|
|
1043
|
+
return { data: null, error: new SupatypeError({ message: "Not authenticated" }) }
|
|
958
1044
|
}
|
|
959
1045
|
const body: Record<string, unknown> = {}
|
|
960
1046
|
if (params.channel !== undefined) body["channel"] = params.channel
|
|
@@ -998,7 +1084,7 @@ export class AuthClient {
|
|
|
998
1084
|
if (this.currentSession === null) {
|
|
999
1085
|
return {
|
|
1000
1086
|
data: { session: null, user: null },
|
|
1001
|
-
error: { message: "Not authenticated" },
|
|
1087
|
+
error: new SupatypeError({ message: "Not authenticated" }),
|
|
1002
1088
|
}
|
|
1003
1089
|
}
|
|
1004
1090
|
const res = await this.send(
|
|
@@ -1058,7 +1144,7 @@ export class AuthClient {
|
|
|
1058
1144
|
error: SupatypeError | null
|
|
1059
1145
|
}> => {
|
|
1060
1146
|
if (this.currentSession === null) {
|
|
1061
|
-
return { data: null, error: { message: "Not authenticated" } }
|
|
1147
|
+
return { data: null, error: new SupatypeError({ message: "Not authenticated" }) }
|
|
1062
1148
|
}
|
|
1063
1149
|
const res = await this.send(
|
|
1064
1150
|
`${this.url}/factors/${params.factorId}`,
|
|
@@ -1086,7 +1172,7 @@ export class AuthClient {
|
|
|
1086
1172
|
}> => {
|
|
1087
1173
|
const userResult = await this.getUser()
|
|
1088
1174
|
if (userResult.error !== null || userResult.data.user === null) {
|
|
1089
|
-
return { data: null, error: userResult.error ?? { message: "Could not get user" } }
|
|
1175
|
+
return { data: null, error: userResult.error ?? new SupatypeError({ message: "Could not get user" }) }
|
|
1090
1176
|
}
|
|
1091
1177
|
const allFactors: Factor[] = userResult.data.user.factors ?? []
|
|
1092
1178
|
const verifiedFactors = allFactors.filter((f) => f.status === "verified")
|
|
@@ -1113,16 +1199,16 @@ export class AuthClient {
|
|
|
1113
1199
|
error: SupatypeError | null
|
|
1114
1200
|
}> => {
|
|
1115
1201
|
if (this.currentSession === null) {
|
|
1116
|
-
return { data: null, error: { message: "Not authenticated" } }
|
|
1202
|
+
return { data: null, error: new SupatypeError({ message: "Not authenticated" }) }
|
|
1117
1203
|
}
|
|
1118
1204
|
// Decode the JWT payload to read the aal and amr claims
|
|
1119
1205
|
const parts = this.currentSession.accessToken.split(".")
|
|
1120
1206
|
if (parts.length !== 3) {
|
|
1121
|
-
return { data: null, error: { message: "Invalid access token format" } }
|
|
1207
|
+
return { data: null, error: new SupatypeError({ message: "Invalid access token format" }) }
|
|
1122
1208
|
}
|
|
1123
1209
|
const payload = decodeJwtPayload(parts[1]!)
|
|
1124
1210
|
if (payload === null) {
|
|
1125
|
-
return { data: null, error: { message: "Failed to decode access token" } }
|
|
1211
|
+
return { data: null, error: new SupatypeError({ message: "Failed to decode access token" }) }
|
|
1126
1212
|
}
|
|
1127
1213
|
const { currentLevel, methods } = assuranceClaims(payload)
|
|
1128
1214
|
try {
|
|
@@ -1133,7 +1219,7 @@ export class AuthClient {
|
|
|
1133
1219
|
const nextLevel = hasVerifiedFactors ? "aal2" : "aal1"
|
|
1134
1220
|
return { data: { currentLevel, nextLevel, currentAuthenticationMethods: methods }, error: null }
|
|
1135
1221
|
} catch {
|
|
1136
|
-
return { data: null, error: { message: "Failed to decode access token" } }
|
|
1222
|
+
return { data: null, error: new SupatypeError({ message: "Failed to decode access token" }) }
|
|
1137
1223
|
}
|
|
1138
1224
|
},
|
|
1139
1225
|
}
|
|
@@ -1213,10 +1299,15 @@ export class AuthClient {
|
|
|
1213
1299
|
return value
|
|
1214
1300
|
}
|
|
1215
1301
|
|
|
1216
|
-
|
|
1302
|
+
/**
|
|
1303
|
+
* Start a PKCE flow: a fresh verifier, kept for the redirect, and the challenge parameters the
|
|
1304
|
+
* auth server takes with it (as `/authorize` query parameters or `/sso` body fields).
|
|
1305
|
+
*/
|
|
1306
|
+
private async beginPkce(): Promise<{ code_challenge: string; code_challenge_method: string }> {
|
|
1217
1307
|
const verifier = generateCodeVerifier()
|
|
1218
1308
|
this.codeVerifier = verifier
|
|
1219
1309
|
await this.writeFlowItem(VERIFIER_SUFFIX, verifier)
|
|
1310
|
+
return { code_challenge: createCodeChallengeS256(verifier), code_challenge_method: PKCE_METHOD_S256 }
|
|
1220
1311
|
}
|
|
1221
1312
|
|
|
1222
1313
|
private async readCodeVerifier(): Promise<string | null> {
|
package/src/bindings.ts
CHANGED
|
@@ -17,6 +17,9 @@ export type {
|
|
|
17
17
|
export { startOAuthRedirect } from "./auth-redirect.js"
|
|
18
18
|
export { isClientFor, isRowsOf, isCallerValue } from "./caller-types.js"
|
|
19
19
|
export { runMutation } from "./mutation-request.js"
|
|
20
|
-
export
|
|
20
|
+
export { reportThrown } from "./thrown-error.js"
|
|
21
|
+
export { latestRowsReader } from "./latest-rows.js"
|
|
22
|
+
export type { RowsReadOptions, RowsView } from "./latest-rows.js"
|
|
23
|
+
export type { MutationOperation, MutationOutcome, MutationRequest, MutationSelect } from "./mutation-request.js"
|
|
21
24
|
export { livePreviewMessage, previewMessageOf, originOf } from "./live-preview.js"
|
|
22
25
|
export type { LivePreviewMessage } from "./live-preview.js"
|
package/src/error-codes-doc.ts
CHANGED
|
@@ -20,7 +20,7 @@ export interface ErrorCodeEntry {
|
|
|
20
20
|
/** HTTP status code typically associated with this error. */
|
|
21
21
|
httpStatus?: number | undefined
|
|
22
22
|
/** Category grouping. */
|
|
23
|
-
category: "auth" | "postgrest" | "storage" | "realtime" | "network"
|
|
23
|
+
category: "auth" | "postgrest" | "storage" | "realtime" | "network" | "setup"
|
|
24
24
|
}
|
|
25
25
|
|
|
26
26
|
export const ERROR_CODES_DOCUMENTATION: readonly ErrorCodeEntry[] = [
|
|
@@ -305,6 +305,14 @@ export const ERROR_CODES_DOCUMENTATION: readonly ErrorCodeEntry[] = [
|
|
|
305
305
|
httpStatus: 429,
|
|
306
306
|
category: "network",
|
|
307
307
|
},
|
|
308
|
+
// ── Setup errors ─────────────────────────────────────────────────────────
|
|
309
|
+
{
|
|
310
|
+
code: "MISSING_CLIENT_VALUES",
|
|
311
|
+
description: "A request was made by a client with no API address or no anon key, so nothing was sent.",
|
|
312
|
+
suggestion:
|
|
313
|
+
"Pass url and anonKey to createClient, set them at build time, or load /supatype-env.js before the app. The error names each value and every name it can be set under.",
|
|
314
|
+
category: "setup",
|
|
315
|
+
},
|
|
308
316
|
] as const
|
|
309
317
|
|
|
310
318
|
/**
|