@gusnips/react 0.9.3 → 0.9.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +18 -0
- package/dist/supabase-session.d.ts +27 -0
- package/dist/supabase-session.d.ts.map +1 -1
- package/dist/supabase-session.js +36 -10
- package/dist/supabase-session.js.map +1 -1
- package/package.json +1 -1
- package/src/supabase-session.test.ts +29 -1
- package/src/supabase-session.ts +36 -9
package/README.md
CHANGED
|
@@ -57,6 +57,24 @@ out. More importantly, it keeps a network failure separate from GoTrue refusing
|
|
|
57
57
|
so losing Wi-Fi does not become a logout. It lives behind a subpath because it imports
|
|
58
58
|
`@supabase/supabase-js`; the main entry does not.
|
|
59
59
|
|
|
60
|
+
The test it makes that decision with is exported, because a refresh is not the only place an app
|
|
61
|
+
asks it:
|
|
62
|
+
|
|
63
|
+
```ts
|
|
64
|
+
import { isAuthOutage } from "@gusnips/react/supabase";
|
|
65
|
+
|
|
66
|
+
// A callback that just failed to trade its one-use link.
|
|
67
|
+
if (isAuthOutage(error))
|
|
68
|
+
showRetry(); // the link was never spent — the same one still works
|
|
69
|
+
else showLinkSpent(); // auth said no: only a new link helps
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
`isAuthOutage` is true when auth FAILED rather than answered — a request that never landed, or a
|
|
73
|
+
5xx. Only a real refusal is an answer, and only an answer may end a session or a link. Use it
|
|
74
|
+
anywhere that decision is made; the sentence you show is a separate question, and a narrower one
|
|
75
|
+
— "check your connection" is true of a request that never landed and false of a 502, which is
|
|
76
|
+
ours. auth-js marks "nothing came back", and only that, with status 0.
|
|
77
|
+
|
|
60
78
|
Supabase email links have two complete patterns. Keep either one, never half of each:
|
|
61
79
|
|
|
62
80
|
- A link carrying `token_hash` needs one explicit `verifyOtp` call. Guard it against React
|
|
@@ -16,6 +16,33 @@ export interface SupabaseSessionAuth {
|
|
|
16
16
|
error: unknown;
|
|
17
17
|
}>;
|
|
18
18
|
}
|
|
19
|
+
/**
|
|
20
|
+
* Auth FAILED rather than answered, so nothing is known about the credential it was handed:
|
|
21
|
+
* either the request never landed, or GoTrue failed on its own.
|
|
22
|
+
*
|
|
23
|
+
* This is the question every site that ends a session has to ask — a token refresh, an API
|
|
24
|
+
* door, and a callback choosing between "try again" and "that link is spent". Without it one
|
|
25
|
+
* bad minute at GoTrue signs out everybody whose token happened to need refreshing, and tells
|
|
26
|
+
* everybody clicking a recovery link that it has expired, which sends them to ask the service
|
|
27
|
+
* that is down for a replacement.
|
|
28
|
+
*
|
|
29
|
+
* Both clauses carry weight, and the second is why this is a function rather than one call.
|
|
30
|
+
* `isAuthRetryableFetchError` is a NAME check, and the statuses that earn that name are a list
|
|
31
|
+
* auth-js owns. The list has drifted — 502, 503 and 504 at 2.91; those plus the 52x family and
|
|
32
|
+
* still no 500 at 2.106; 500 through 530 at 2.108 — so code leaning on it alone is right at
|
|
33
|
+
* whichever version happens to be installed, which is not the same as being right, and a fleet
|
|
34
|
+
* installs several versions at once. It is also a LIST rather than a range, so it has holes at
|
|
35
|
+
* every version, drift or no drift: at 2.112 nothing covers 505 through 519 or 531 up, and a
|
|
36
|
+
* 507 or a 599 out of a proxy in front of GoTrue arrives as a plain `AuthApiError`. Pinning a
|
|
37
|
+
* recent SDK does not make the status clause redundant.
|
|
38
|
+
*
|
|
39
|
+
* Read the version a tree really serves with `bun why @supabase/auth-js`, which prints one
|
|
40
|
+
* heading per RESOLVED version; `node_modules` keeps copies the resolver does not serve.
|
|
41
|
+
*
|
|
42
|
+
* Anything that is not an auth error at all answers true: a failure from outside auth-js is not
|
|
43
|
+
* a verdict on the credential either.
|
|
44
|
+
*/
|
|
45
|
+
export declare function isAuthOutage(error: unknown): boolean;
|
|
19
46
|
/**
|
|
20
47
|
* Connects a Supabase auth client to {@link SessionAdapter}.
|
|
21
48
|
*
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"supabase-session.d.ts","sourceRoot":"","sources":["../src/supabase-session.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,iBAAiB,CAAC;AAEtD,UAAU,qBAAqB;IAC7B,IAAI,EAAE;QAAE,OAAO,EAAE;YAAE,YAAY,EAAE,MAAM,CAAA;SAAE,GAAG,IAAI,CAAA;KAAE,CAAC;CACpD;AAED,8FAA8F;AAC9F,MAAM,WAAW,mBAAmB;IAClC,UAAU,IAAI,OAAO,CAAC,qBAAqB,CAAC,CAAC;IAC7C,cAAc,IAAI,OAAO,CAAC,qBAAqB,GAAG;QAAE,KAAK,EAAE,OAAO,CAAA;KAAE,CAAC,CAAC;IACtE,OAAO,IAAI,OAAO,CAAC;QAAE,KAAK,EAAE,OAAO,CAAA;KAAE,CAAC,CAAC;CACxC;
|
|
1
|
+
{"version":3,"file":"supabase-session.d.ts","sourceRoot":"","sources":["../src/supabase-session.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,iBAAiB,CAAC;AAEtD,UAAU,qBAAqB;IAC7B,IAAI,EAAE;QAAE,OAAO,EAAE;YAAE,YAAY,EAAE,MAAM,CAAA;SAAE,GAAG,IAAI,CAAA;KAAE,CAAC;CACpD;AAED,8FAA8F;AAC9F,MAAM,WAAW,mBAAmB;IAClC,UAAU,IAAI,OAAO,CAAC,qBAAqB,CAAC,CAAC;IAC7C,cAAc,IAAI,OAAO,CAAC,qBAAqB,GAAG;QAAE,KAAK,EAAE,OAAO,CAAA;KAAE,CAAC,CAAC;IACtE,OAAO,IAAI,OAAO,CAAC;QAAE,KAAK,EAAE,OAAO,CAAA;KAAE,CAAC,CAAC;CACxC;AAOD;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AACH,wBAAgB,YAAY,CAAC,KAAK,EAAE,OAAO,GAAG,OAAO,CAGpD;AAaD;;;;;;;GAOG;AACH,wBAAgB,4BAA4B,CAAC,IAAI,EAAE,mBAAmB,GAAG,cAAc,CAsBtF"}
|
package/dist/supabase-session.js
CHANGED
|
@@ -1,15 +1,41 @@
|
|
|
1
1
|
import { isAuthError, isAuthRetryableFetchError } from "@supabase/supabase-js";
|
|
2
|
+
/**
|
|
3
|
+
* Auth FAILED rather than answered, so nothing is known about the credential it was handed:
|
|
4
|
+
* either the request never landed, or GoTrue failed on its own.
|
|
5
|
+
*
|
|
6
|
+
* This is the question every site that ends a session has to ask — a token refresh, an API
|
|
7
|
+
* door, and a callback choosing between "try again" and "that link is spent". Without it one
|
|
8
|
+
* bad minute at GoTrue signs out everybody whose token happened to need refreshing, and tells
|
|
9
|
+
* everybody clicking a recovery link that it has expired, which sends them to ask the service
|
|
10
|
+
* that is down for a replacement.
|
|
11
|
+
*
|
|
12
|
+
* Both clauses carry weight, and the second is why this is a function rather than one call.
|
|
13
|
+
* `isAuthRetryableFetchError` is a NAME check, and the statuses that earn that name are a list
|
|
14
|
+
* auth-js owns. The list has drifted — 502, 503 and 504 at 2.91; those plus the 52x family and
|
|
15
|
+
* still no 500 at 2.106; 500 through 530 at 2.108 — so code leaning on it alone is right at
|
|
16
|
+
* whichever version happens to be installed, which is not the same as being right, and a fleet
|
|
17
|
+
* installs several versions at once. It is also a LIST rather than a range, so it has holes at
|
|
18
|
+
* every version, drift or no drift: at 2.112 nothing covers 505 through 519 or 531 up, and a
|
|
19
|
+
* 507 or a 599 out of a proxy in front of GoTrue arrives as a plain `AuthApiError`. Pinning a
|
|
20
|
+
* recent SDK does not make the status clause redundant.
|
|
21
|
+
*
|
|
22
|
+
* Read the version a tree really serves with `bun why @supabase/auth-js`, which prints one
|
|
23
|
+
* heading per RESOLVED version; `node_modules` keeps copies the resolver does not serve.
|
|
24
|
+
*
|
|
25
|
+
* Anything that is not an auth error at all answers true: a failure from outside auth-js is not
|
|
26
|
+
* a verdict on the credential either.
|
|
27
|
+
*/
|
|
28
|
+
export function isAuthOutage(error) {
|
|
29
|
+
if (!isAuthError(error))
|
|
30
|
+
return true;
|
|
31
|
+
return isAuthRetryableFetchError(error) || (error.status ?? 0) >= 500;
|
|
32
|
+
}
|
|
2
33
|
function reachedAuth(error) {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
//
|
|
6
|
-
//
|
|
7
|
-
|
|
8
|
-
// it alone is right at whichever version happens to be installed, which is not the same as
|
|
9
|
-
// being right. Without this clause one bad minute at GoTrue signs out everybody whose token
|
|
10
|
-
// happened to need refreshing. Measured across the fleet: four backends answered 401 to
|
|
11
|
-
// their own auth provider's 500 for exactly this reason.
|
|
12
|
-
if ((error.status ?? 0) >= 500)
|
|
34
|
+
// Measured across the fleet: four backends answered 401 to their own auth provider's 500,
|
|
35
|
+
// and every client reads a 401 as a dead session, so one bad minute at auth signed out
|
|
36
|
+
// everybody who was signed in. `isAuthError` leads only for the narrowing below —
|
|
37
|
+
// `isAuthOutage` already answers true for anything that is not an auth error.
|
|
38
|
+
if (!isAuthError(error) || isAuthOutage(error))
|
|
13
39
|
return false;
|
|
14
40
|
// Newer clients name a refresh that reached GoTrue but was deliberately discarded because the
|
|
15
41
|
// local session changed mid-flight. That race is a no-op, not proof that either session died.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"supabase-session.js","sourceRoot":"","sources":["../src/supabase-session.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,yBAAyB,EAAuB,MAAM,uBAAuB,CAAC;AAmBpG,
|
|
1
|
+
{"version":3,"file":"supabase-session.js","sourceRoot":"","sources":["../src/supabase-session.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,yBAAyB,EAAuB,MAAM,uBAAuB,CAAC;AAmBpG;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AACH,MAAM,UAAU,YAAY,CAAC,KAAc;IACzC,IAAI,CAAC,WAAW,CAAC,KAAK,CAAC;QAAE,OAAO,IAAI,CAAC;IACrC,OAAO,yBAAyB,CAAC,KAAK,CAAC,IAAI,CAAC,KAAK,CAAC,MAAM,IAAI,CAAC,CAAC,IAAI,GAAG,CAAC;AACxE,CAAC;AAED,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"@gusnips/react",
|
|
3
|
-
"version": "0.9.
|
|
3
|
+
"version": "0.9.4",
|
|
4
4
|
"description": "The headless runtime under a Vite + React SPA: typed fetch client with single-flight refresh, auth store, query defaults, route guards, error boundary, stale-tab recovery, i18n bootstrap.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
@@ -4,7 +4,11 @@ import {
|
|
|
4
4
|
AuthRetryableFetchError,
|
|
5
5
|
} from "@supabase/supabase-js";
|
|
6
6
|
import { describe, expect, it, vi } from "vitest";
|
|
7
|
-
import {
|
|
7
|
+
import {
|
|
8
|
+
createSupabaseSessionAdapter,
|
|
9
|
+
isAuthOutage,
|
|
10
|
+
type SupabaseSessionAuth,
|
|
11
|
+
} from "./supabase-session.ts";
|
|
8
12
|
|
|
9
13
|
function auth(overrides: Partial<SupabaseSessionAuth> = {}): SupabaseSessionAuth {
|
|
10
14
|
return {
|
|
@@ -103,3 +107,27 @@ describe("createSupabaseSessionAdapter", () => {
|
|
|
103
107
|
expect(signOut).toHaveBeenCalledOnce();
|
|
104
108
|
});
|
|
105
109
|
});
|
|
110
|
+
|
|
111
|
+
describe("isAuthOutage", () => {
|
|
112
|
+
it("lets a real refusal through as an answer", () => {
|
|
113
|
+
expect(
|
|
114
|
+
isAuthOutage(new AuthApiError("Invalid login credentials", 400, "invalid_credentials")),
|
|
115
|
+
).toBe(false);
|
|
116
|
+
expect(
|
|
117
|
+
isAuthOutage(new AuthApiError("Token has expired or is invalid", 403, "otp_expired")),
|
|
118
|
+
).toBe(false);
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
it("refuses to read a failure as an answer", () => {
|
|
122
|
+
// Nothing came back, and the two shapes that carries.
|
|
123
|
+
expect(isAuthOutage(new AuthRetryableFetchError("Failed to fetch", 0))).toBe(true);
|
|
124
|
+
expect(isAuthOutage(new TypeError("Load failed"))).toBe(true);
|
|
125
|
+
// A gateway between us and GoTrue, which auth-js wraps by name.
|
|
126
|
+
expect(isAuthOutage(new AuthRetryableFetchError("Bad Gateway", 502))).toBe(true);
|
|
127
|
+
// And the reason the status clause is not redundant: the statuses earning that name are a
|
|
128
|
+
// LIST, so it has holes at every version. 507 and 599 are on no version of it, and arrive
|
|
129
|
+
// as ordinary AuthApiErrors that `isAuthRetryableFetchError` answers false for.
|
|
130
|
+
expect(isAuthOutage(new AuthApiError("Insufficient Storage", 507, undefined))).toBe(true);
|
|
131
|
+
expect(isAuthOutage(new AuthApiError("Network Connect Timeout", 599, undefined))).toBe(true);
|
|
132
|
+
});
|
|
133
|
+
});
|
package/src/supabase-session.ts
CHANGED
|
@@ -17,16 +17,43 @@ type _SupabaseAuthFitsAdapter = Satisfied<
|
|
|
17
17
|
SupabaseClient["auth"] extends SupabaseSessionAuth ? true : false
|
|
18
18
|
>;
|
|
19
19
|
|
|
20
|
+
/**
|
|
21
|
+
* Auth FAILED rather than answered, so nothing is known about the credential it was handed:
|
|
22
|
+
* either the request never landed, or GoTrue failed on its own.
|
|
23
|
+
*
|
|
24
|
+
* This is the question every site that ends a session has to ask — a token refresh, an API
|
|
25
|
+
* door, and a callback choosing between "try again" and "that link is spent". Without it one
|
|
26
|
+
* bad minute at GoTrue signs out everybody whose token happened to need refreshing, and tells
|
|
27
|
+
* everybody clicking a recovery link that it has expired, which sends them to ask the service
|
|
28
|
+
* that is down for a replacement.
|
|
29
|
+
*
|
|
30
|
+
* Both clauses carry weight, and the second is why this is a function rather than one call.
|
|
31
|
+
* `isAuthRetryableFetchError` is a NAME check, and the statuses that earn that name are a list
|
|
32
|
+
* auth-js owns. The list has drifted — 502, 503 and 504 at 2.91; those plus the 52x family and
|
|
33
|
+
* still no 500 at 2.106; 500 through 530 at 2.108 — so code leaning on it alone is right at
|
|
34
|
+
* whichever version happens to be installed, which is not the same as being right, and a fleet
|
|
35
|
+
* installs several versions at once. It is also a LIST rather than a range, so it has holes at
|
|
36
|
+
* every version, drift or no drift: at 2.112 nothing covers 505 through 519 or 531 up, and a
|
|
37
|
+
* 507 or a 599 out of a proxy in front of GoTrue arrives as a plain `AuthApiError`. Pinning a
|
|
38
|
+
* recent SDK does not make the status clause redundant.
|
|
39
|
+
*
|
|
40
|
+
* Read the version a tree really serves with `bun why @supabase/auth-js`, which prints one
|
|
41
|
+
* heading per RESOLVED version; `node_modules` keeps copies the resolver does not serve.
|
|
42
|
+
*
|
|
43
|
+
* Anything that is not an auth error at all answers true: a failure from outside auth-js is not
|
|
44
|
+
* a verdict on the credential either.
|
|
45
|
+
*/
|
|
46
|
+
export function isAuthOutage(error: unknown): boolean {
|
|
47
|
+
if (!isAuthError(error)) return true;
|
|
48
|
+
return isAuthRetryableFetchError(error) || (error.status ?? 0) >= 500;
|
|
49
|
+
}
|
|
50
|
+
|
|
20
51
|
function reachedAuth(error: unknown): boolean {
|
|
21
|
-
|
|
22
|
-
//
|
|
23
|
-
//
|
|
24
|
-
//
|
|
25
|
-
|
|
26
|
-
// being right. Without this clause one bad minute at GoTrue signs out everybody whose token
|
|
27
|
-
// happened to need refreshing. Measured across the fleet: four backends answered 401 to
|
|
28
|
-
// their own auth provider's 500 for exactly this reason.
|
|
29
|
-
if ((error.status ?? 0) >= 500) return false;
|
|
52
|
+
// Measured across the fleet: four backends answered 401 to their own auth provider's 500,
|
|
53
|
+
// and every client reads a 401 as a dead session, so one bad minute at auth signed out
|
|
54
|
+
// everybody who was signed in. `isAuthError` leads only for the narrowing below —
|
|
55
|
+
// `isAuthOutage` already answers true for anything that is not an auth error.
|
|
56
|
+
if (!isAuthError(error) || isAuthOutage(error)) return false;
|
|
30
57
|
// Newer clients name a refresh that reached GoTrue but was deliberately discarded because the
|
|
31
58
|
// local session changed mid-flight. That race is a no-op, not proof that either session died.
|
|
32
59
|
return error.name !== "AuthRefreshDiscardedError";
|