@snokam/auth 6.3.10 → 6.3.12
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/CHANGELOG.md +12 -0
- package/package.json +3 -3
- package/src/employee-query/retry.test.ts +31 -0
- package/src/employee-query/retry.ts +7 -1
- package/src/hooks.ts +2 -6
- package/src/session/confirm.test.ts +35 -0
- package/src/session/confirm.ts +16 -0
- package/src/session/confirmed.tsx +2 -2
- package/src/theme/saved-theme-sync.tsx +9 -3
package/CHANGELOG.md
CHANGED
|
@@ -3,6 +3,18 @@
|
|
|
3
3
|
All notable changes to this project will be documented in this file.
|
|
4
4
|
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
|
5
5
|
|
|
6
|
+
## [6.3.12](https://github.com/snokam/components/compare/%40snokam%2Fauth%406.3.11...%40snokam%2Fauth%406.3.12) (2026-11-14)
|
|
7
|
+
|
|
8
|
+
### Bug Fixes
|
|
9
|
+
|
|
10
|
+
- **auth:** stop retrying /me on client errors and probing with the wrong identity ([#3708](https://github.com/snokam/components/issues/3708)) ([1bf6163](https://github.com/snokam/components/commit/1bf616328370880feaf9c50a4972c4553a0eb1b8))
|
|
11
|
+
|
|
12
|
+
## [6.3.11](https://github.com/snokam/components/compare/%40snokam%2Fauth%406.3.10...%40snokam%2Fauth%406.3.11) (2026-11-13)
|
|
13
|
+
|
|
14
|
+
### Performance Improvements
|
|
15
|
+
|
|
16
|
+
- **tasks:** the task page loads without waiting for the review queue ([#3689](https://github.com/snokam/components/issues/3689)) ([afadbbe](https://github.com/snokam/components/commit/afadbbefc94ac8e81a3971be522ad93e2a2bba74))
|
|
17
|
+
|
|
6
18
|
## [6.3.10](https://github.com/snokam/components/compare/%40snokam%2Fauth%406.3.9...%40snokam%2Fauth%406.3.10) (2026-11-13)
|
|
7
19
|
|
|
8
20
|
**Note:** Version bump only for package @snokam/auth
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@snokam/auth",
|
|
3
3
|
"description": "Snøkam auth components",
|
|
4
|
-
"version": "6.3.
|
|
4
|
+
"version": "6.3.12",
|
|
5
5
|
"main": "src/index.ts",
|
|
6
6
|
"sideEffects": false,
|
|
7
7
|
"types": "src/index.ts",
|
|
@@ -28,9 +28,9 @@
|
|
|
28
28
|
"path-to-regexp": "^9.0.0",
|
|
29
29
|
"@snokam/api-client": "1.0.0",
|
|
30
30
|
"@snokam/identity": "1.1.1",
|
|
31
|
-
"@snokam/page-content": "6.12.
|
|
31
|
+
"@snokam/page-content": "6.12.9",
|
|
32
32
|
"@snokam/react-query": "1.1.3",
|
|
33
|
-
"@snokam/tenant": "4.5.
|
|
33
|
+
"@snokam/tenant": "4.5.8",
|
|
34
34
|
"@snokam/ui": "7.11.0"
|
|
35
35
|
},
|
|
36
36
|
"peerDependencies": {
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import assert from "node:assert/strict";
|
|
2
|
+
import { describe, it } from "node:test";
|
|
3
|
+
import { retryUnlessMissing } from "./retry.ts";
|
|
4
|
+
|
|
5
|
+
function failed(status: number) {
|
|
6
|
+
return { response: { status } };
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
describe("retrying the signed-in employee", () => {
|
|
10
|
+
for (const status of [400, 401, 403, 404, 409, 422]) {
|
|
11
|
+
it(`gives up at once on ${status}, which the next try would repeat`, () => {
|
|
12
|
+
assert.equal(retryUnlessMissing(0, failed(status)), false);
|
|
13
|
+
});
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
for (const status of [408, 429, 500, 502, 503]) {
|
|
17
|
+
it(`tries again on ${status}`, () => {
|
|
18
|
+
assert.equal(retryUnlessMissing(0, failed(status)), true);
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
it("tries again when the request never got an answer", () => {
|
|
23
|
+
assert.equal(retryUnlessMissing(0, new TypeError("Failed to fetch")), true);
|
|
24
|
+
assert.equal(retryUnlessMissing(0, null), true);
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
it("stops after three tries", () => {
|
|
28
|
+
assert.equal(retryUnlessMissing(2, failed(503)), true);
|
|
29
|
+
assert.equal(retryUnlessMissing(3, failed(503)), false);
|
|
30
|
+
});
|
|
31
|
+
});
|
|
@@ -1,9 +1,15 @@
|
|
|
1
1
|
const MAX_RETRIES = 3;
|
|
2
|
+
const RETRIED_CLIENT_ERRORS = new Set([408, 429]);
|
|
2
3
|
|
|
3
4
|
function responseStatus(error: unknown): number | undefined {
|
|
4
5
|
return (error as { response?: { status?: number } } | null)?.response?.status;
|
|
5
6
|
}
|
|
6
7
|
|
|
8
|
+
function isLasting(status: number | undefined): boolean {
|
|
9
|
+
if (status === undefined || RETRIED_CLIENT_ERRORS.has(status)) return false;
|
|
10
|
+
return status >= 400 && status < 500;
|
|
11
|
+
}
|
|
12
|
+
|
|
7
13
|
export function retryUnlessMissing(failureCount: number, error: unknown) {
|
|
8
|
-
return responseStatus(error)
|
|
14
|
+
return !isLasting(responseStatus(error)) && failureCount < MAX_RETRIES;
|
|
9
15
|
}
|
package/src/hooks.ts
CHANGED
|
@@ -7,11 +7,7 @@ import { leaveEndedSession } from "./impersonation/ended";
|
|
|
7
7
|
import { useOwnIdentity } from "./identity/own";
|
|
8
8
|
import { useSession } from "./session/useSession";
|
|
9
9
|
import { isReusable, type CachedToken } from "./token/reuse";
|
|
10
|
-
import {
|
|
11
|
-
confirmSessionState,
|
|
12
|
-
fetchSessionFromServer,
|
|
13
|
-
withRetries,
|
|
14
|
-
} from "./session/confirm";
|
|
10
|
+
import { confirmServerSession, withRetries } from "./session/confirm";
|
|
15
11
|
import {
|
|
16
12
|
ConsentRequiredError,
|
|
17
13
|
failureOf,
|
|
@@ -112,7 +108,7 @@ export function fetchOwnToken(): Promise<string> {
|
|
|
112
108
|
}
|
|
113
109
|
|
|
114
110
|
async function signInAgainIfSignedOut(error: SignedOutError): Promise<void> {
|
|
115
|
-
const state = await
|
|
111
|
+
const state = await confirmServerSession();
|
|
116
112
|
if (state !== "signed-out") throw error;
|
|
117
113
|
const here = `${window.location.pathname}${window.location.search}`;
|
|
118
114
|
window.location.href = `/auth/login?callbackUrl=${encodeURIComponent(here)}`;
|
|
@@ -3,6 +3,7 @@ import assert from "node:assert/strict";
|
|
|
3
3
|
import {
|
|
4
4
|
confirmSessionState,
|
|
5
5
|
readSessionState,
|
|
6
|
+
sharedConfirmation,
|
|
6
7
|
withRetries,
|
|
7
8
|
} from "./confirm.ts";
|
|
8
9
|
|
|
@@ -98,3 +99,37 @@ test("errors that are not transient are not retried", async () => {
|
|
|
98
99
|
);
|
|
99
100
|
assert.equal(attempts, 1);
|
|
100
101
|
});
|
|
102
|
+
|
|
103
|
+
test("callers asking at the same time share one session check", async () => {
|
|
104
|
+
let checks = 0;
|
|
105
|
+
let answer: (state: "signed-in") => void = () => {};
|
|
106
|
+
const confirm = sharedConfirmation(() => {
|
|
107
|
+
checks += 1;
|
|
108
|
+
return new Promise((resolve) => {
|
|
109
|
+
answer = resolve;
|
|
110
|
+
});
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
const first = confirm();
|
|
114
|
+
const second = confirm();
|
|
115
|
+
answer("signed-in");
|
|
116
|
+
|
|
117
|
+
assert.deepEqual(await Promise.all([first, second]), [
|
|
118
|
+
"signed-in",
|
|
119
|
+
"signed-in",
|
|
120
|
+
]);
|
|
121
|
+
assert.equal(checks, 1);
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
test("a later caller gets a fresh session check", async () => {
|
|
125
|
+
let checks = 0;
|
|
126
|
+
const confirm = sharedConfirmation(async () => {
|
|
127
|
+
checks += 1;
|
|
128
|
+
return "signed-in";
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
await confirm();
|
|
132
|
+
await confirm();
|
|
133
|
+
|
|
134
|
+
assert.equal(checks, 2);
|
|
135
|
+
});
|
package/src/session/confirm.ts
CHANGED
|
@@ -61,3 +61,19 @@ export async function confirmSessionState(
|
|
|
61
61
|
|
|
62
62
|
export const fetchSessionFromServer = () =>
|
|
63
63
|
fetch("/api/auth/session", { credentials: "include", cache: "no-store" });
|
|
64
|
+
|
|
65
|
+
export function sharedConfirmation(
|
|
66
|
+
confirm: () => Promise<SessionState>
|
|
67
|
+
): () => Promise<SessionState> {
|
|
68
|
+
let pending: Promise<SessionState> | null = null;
|
|
69
|
+
return () => {
|
|
70
|
+
pending ??= confirm().finally(() => {
|
|
71
|
+
pending = null;
|
|
72
|
+
});
|
|
73
|
+
return pending;
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
export const confirmServerSession = sharedConfirmation(() =>
|
|
78
|
+
confirmSessionState(fetchSessionFromServer)
|
|
79
|
+
);
|
|
@@ -6,7 +6,7 @@ import {
|
|
|
6
6
|
type SessionContextValue,
|
|
7
7
|
} from "next-auth/react";
|
|
8
8
|
import type { Session } from "next-auth";
|
|
9
|
-
import {
|
|
9
|
+
import { confirmServerSession } from "./confirm";
|
|
10
10
|
import { holdSession } from "./hold";
|
|
11
11
|
|
|
12
12
|
export const ConfirmedSessionContext =
|
|
@@ -32,7 +32,7 @@ export function ConfirmedSessionProvider({
|
|
|
32
32
|
if (status !== "unauthenticated" || !lastSignedIn || confirmedSignedOut)
|
|
33
33
|
return;
|
|
34
34
|
let cancelled = false;
|
|
35
|
-
|
|
35
|
+
confirmServerSession().then((state) => {
|
|
36
36
|
if (!cancelled && state === "signed-out") setConfirmedSignedOut(true);
|
|
37
37
|
});
|
|
38
38
|
return () => {
|
|
@@ -2,7 +2,11 @@
|
|
|
2
2
|
|
|
3
3
|
import { useEffect, useMemo } from "react";
|
|
4
4
|
import { Configuration, EmployeesClient } from "@snokam/api-client/employees";
|
|
5
|
-
import {
|
|
5
|
+
import {
|
|
6
|
+
apiBaseUrl,
|
|
7
|
+
apiServesPlatformTemplate,
|
|
8
|
+
useOptionalConfig,
|
|
9
|
+
} from "@snokam/tenant";
|
|
6
10
|
import { useClientQuery } from "@snokam/react-query";
|
|
7
11
|
import { useAdminTheme } from "@snokam/ui/theme/admin";
|
|
8
12
|
import type { ColorSchemeMode } from "@snokam/ui/theme/admin";
|
|
@@ -21,6 +25,8 @@ export default function SavedThemeSync() {
|
|
|
21
25
|
const config = useOptionalConfig();
|
|
22
26
|
|
|
23
27
|
const basePath = config ? apiBaseUrl(config) : undefined;
|
|
28
|
+
const hasEmployees =
|
|
29
|
+
!!config && !!basePath && !apiServesPlatformTemplate(config);
|
|
24
30
|
const client = useMemo(
|
|
25
31
|
() =>
|
|
26
32
|
new EmployeesClient(
|
|
@@ -38,13 +44,13 @@ export default function SavedThemeSync() {
|
|
|
38
44
|
}).useGetMeProtectedQuery(
|
|
39
45
|
{},
|
|
40
46
|
{
|
|
41
|
-
enabled: isAuthenticated &&
|
|
47
|
+
enabled: isAuthenticated && hasEmployees,
|
|
42
48
|
retry: retryUnlessMissing,
|
|
43
49
|
}
|
|
44
50
|
);
|
|
45
51
|
|
|
46
52
|
const saved = data?.settings?.colorScheme;
|
|
47
|
-
const settled = isSuccess || isError || !
|
|
53
|
+
const settled = isSuccess || isError || !hasEmployees;
|
|
48
54
|
|
|
49
55
|
useEffect(() => {
|
|
50
56
|
if (isMode(saved)) seedMode(saved);
|