oidc-spa 8.2.9 → 8.2.11
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 +236 -79
- package/core/createOidc.js +4 -2
- package/core/createOidc.js.map +1 -1
- package/esm/core/createOidc.js +4 -2
- package/esm/core/createOidc.js.map +1 -1
- package/esm/react-spa/createOidcSpaApi.js +1 -1
- package/esm/react-spa/createOidcSpaApi.js.map +1 -1
- package/esm/react-spa/types.d.ts +5 -5
- package/esm/tanstack-start/react/createOidcSpaApi.js +107 -88
- package/esm/tanstack-start/react/createOidcSpaApi.js.map +1 -1
- package/esm/tanstack-start/react/types.d.ts +50 -27
- package/esm/tools/MaybeAsync.d.ts +1 -0
- package/esm/tools/MaybeAsync.js +2 -0
- package/esm/tools/MaybeAsync.js.map +1 -0
- package/package.json +1 -1
- package/react-spa/createOidcSpaApi.js +1 -1
- package/react-spa/createOidcSpaApi.js.map +1 -1
- package/react-spa/types.d.ts +5 -5
- package/src/core/createOidc.ts +3 -1
- package/src/react-spa/createOidcSpaApi.tsx +1 -1
- package/src/react-spa/types.tsx +5 -5
- package/src/tanstack-start/react/createOidcSpaApi.tsx +155 -156
- package/src/tanstack-start/react/types.tsx +59 -35
- package/src/tools/MaybeAsync.ts +1 -0
- package/tools/MaybeAsync.d.ts +1 -0
- package/tools/MaybeAsync.js +3 -0
- package/tools/MaybeAsync.js.map +1 -0
|
@@ -1,15 +1,8 @@
|
|
|
1
|
-
import {
|
|
2
|
-
useState,
|
|
3
|
-
useEffect,
|
|
4
|
-
type ReactNode,
|
|
5
|
-
createContext,
|
|
6
|
-
useContext,
|
|
7
|
-
type ComponentType
|
|
8
|
-
} from "react";
|
|
1
|
+
import { useState, useEffect, type ReactNode, useReducer, type ComponentType } from "react";
|
|
9
2
|
import type {
|
|
10
3
|
CreateValidateAndGetAccessTokenClaims,
|
|
11
4
|
OidcSpaApi,
|
|
12
|
-
|
|
5
|
+
UseOidc,
|
|
13
6
|
GetOidc,
|
|
14
7
|
ParamsOfBootstrap,
|
|
15
8
|
OidcServerContext
|
|
@@ -31,6 +24,7 @@ import { getRequest, setResponseHeader, setResponseStatus } from "@tanstack/reac
|
|
|
31
24
|
import { toFullyQualifiedUrl } from "../../tools/toFullyQualifiedUrl";
|
|
32
25
|
import { UnifiedClientRetryForSsrLoadersError } from "./rfcUnifiedClientRetryForSsrLoaders/UnifiedClientRetryForSsrLoadersError";
|
|
33
26
|
import { setDesiredPostLoginRedirectUrl } from "../../core/desiredPostLoginRedirectUrl";
|
|
27
|
+
import type { MaybeAsync } from "../../tools/MaybeAsync";
|
|
34
28
|
|
|
35
29
|
export function createOidcSpaApi<
|
|
36
30
|
AutoLogin extends boolean,
|
|
@@ -61,11 +55,11 @@ export function createOidcSpaApi<
|
|
|
61
55
|
Oidc_core<DecodedIdToken> | OidcInitializationError
|
|
62
56
|
>();
|
|
63
57
|
|
|
64
|
-
const evtAutoLogoutState = createStatefulEvt<
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
58
|
+
const evtAutoLogoutState = createStatefulEvt<UseOidc.Oidc.LoggedIn<unknown>["autoLogoutState"]>(
|
|
59
|
+
() => ({
|
|
60
|
+
shouldDisplayWarning: false
|
|
61
|
+
})
|
|
62
|
+
);
|
|
69
63
|
|
|
70
64
|
dOidcCoreOrInitializationError.pr.then(oidcCoreOrInitializationError => {
|
|
71
65
|
const { hasResolved, value: paramsOfBootstrap } = dParamsOfBootstrap.getState();
|
|
@@ -93,7 +87,7 @@ export function createOidcSpaApi<
|
|
|
93
87
|
}
|
|
94
88
|
|
|
95
89
|
oidcCore.subscribeToAutoLogoutCountdown(({ secondsLeft }) => {
|
|
96
|
-
const newState:
|
|
90
|
+
const newState: UseOidc.Oidc.LoggedIn<unknown>["autoLogoutState"] = (() => {
|
|
97
91
|
if (secondsLeft === undefined) {
|
|
98
92
|
return {
|
|
99
93
|
shouldDisplayWarning: false
|
|
@@ -120,13 +114,116 @@ export function createOidcSpaApi<
|
|
|
120
114
|
});
|
|
121
115
|
});
|
|
122
116
|
|
|
123
|
-
function useOidc(
|
|
117
|
+
function useOidc(params?: {
|
|
118
|
+
assert?: "user logged in" | "user not logged in" | "init completed";
|
|
119
|
+
}): UseOidc.Oidc<DecodedIdToken> {
|
|
120
|
+
const { assert: assert_params } = params ?? {};
|
|
121
|
+
|
|
122
|
+
/*
|
|
123
|
+
if (!isBrowser) {
|
|
124
|
+
throw new Error(
|
|
125
|
+
[
|
|
126
|
+
"oidc-spa: useOidc() can't be used on the server.",
|
|
127
|
+
"You can prevent this component from rendering on the server",
|
|
128
|
+
"by wrapping it into <OidcInitializationGate />"
|
|
129
|
+
].join(" ")
|
|
130
|
+
);
|
|
131
|
+
}
|
|
132
|
+
*/
|
|
133
|
+
|
|
124
134
|
const { hasResolved, value: oidcCore } = dOidcCoreOrInitializationError.getState();
|
|
125
135
|
|
|
126
|
-
|
|
127
|
-
|
|
136
|
+
if (oidcCore instanceof OidcInitializationError) {
|
|
137
|
+
throw oidcCore;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
check_assertion: {
|
|
141
|
+
if (assert_params === undefined) {
|
|
142
|
+
break check_assertion;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
if (assert_params === "init completed") {
|
|
146
|
+
if (!hasResolved) {
|
|
147
|
+
throw new Error(
|
|
148
|
+
[
|
|
149
|
+
"oidc-spa: There is a logic error in the application.",
|
|
150
|
+
"you called useOidc({ assert: 'init completed' }) but",
|
|
151
|
+
...(isBrowser
|
|
152
|
+
? [
|
|
153
|
+
"the component making this call was rendered before",
|
|
154
|
+
"the oidc initialization has completed."
|
|
155
|
+
]
|
|
156
|
+
: ["we are on the server, this assertion will always be wrong."]),
|
|
157
|
+
"\nTo avoid this error make sure you call this within",
|
|
158
|
+
"<OidcInitializationGate /> or remove the assertion and check",
|
|
159
|
+
"the hasInitCompleted property."
|
|
160
|
+
].join(" ")
|
|
161
|
+
);
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
break check_assertion;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
if (!hasResolved) {
|
|
168
|
+
throw new Error(
|
|
169
|
+
[
|
|
170
|
+
"oidc-spa: There is a logic error in the application.",
|
|
171
|
+
`you called useOidc({ assert: "${assert_params}" }) but`,
|
|
172
|
+
"initialization hasn't completed yet, we don't know yet if the",
|
|
173
|
+
"user is logged in or not."
|
|
174
|
+
].join(" ")
|
|
175
|
+
);
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
const getMessage = (v: string) =>
|
|
179
|
+
[
|
|
180
|
+
"oidc-spa: There is a logic error in the application.",
|
|
181
|
+
`If this component is mounted the user is supposed ${v}.`,
|
|
182
|
+
"An explicit assertion was made in this sense."
|
|
183
|
+
].join(" ");
|
|
184
|
+
|
|
185
|
+
switch (assert_params) {
|
|
186
|
+
case "user logged in":
|
|
187
|
+
if (!oidcCore.isUserLoggedIn) {
|
|
188
|
+
throw new Error(getMessage("to be logged in but currently they arn't"));
|
|
189
|
+
}
|
|
190
|
+
break;
|
|
191
|
+
case "user not logged in":
|
|
192
|
+
if (oidcCore.isUserLoggedIn) {
|
|
193
|
+
throw new Error(getMessage("not to be logged in but currently they are"));
|
|
194
|
+
}
|
|
195
|
+
break;
|
|
196
|
+
default:
|
|
197
|
+
assert<Equals<typeof assert_params, never>>(false);
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
const [, reRender] = useReducer(n => n + 1, 0);
|
|
202
|
+
|
|
203
|
+
useEffect(() => {
|
|
204
|
+
if (hasResolved) {
|
|
205
|
+
return;
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
let isActive = true;
|
|
209
|
+
|
|
210
|
+
dOidcCoreOrInitializationError.pr.then(() => {
|
|
211
|
+
if (!isActive) {
|
|
212
|
+
return;
|
|
213
|
+
}
|
|
214
|
+
reRender();
|
|
215
|
+
});
|
|
216
|
+
|
|
217
|
+
return () => {
|
|
218
|
+
isActive = false;
|
|
219
|
+
};
|
|
220
|
+
}, []);
|
|
128
221
|
|
|
129
222
|
const [, reRenderIfDecodedIdTokenChanged] = useState<DecodedIdToken | undefined>(() => {
|
|
223
|
+
if (oidcCore === undefined) {
|
|
224
|
+
return undefined;
|
|
225
|
+
}
|
|
226
|
+
|
|
130
227
|
if (!oidcCore.isUserLoggedIn) {
|
|
131
228
|
return undefined;
|
|
132
229
|
}
|
|
@@ -136,6 +233,10 @@ export function createOidcSpaApi<
|
|
|
136
233
|
const [evtIsDecodedIdTokenUsed] = useState(() => createStatefulEvt<boolean>(() => false));
|
|
137
234
|
|
|
138
235
|
useEffect(() => {
|
|
236
|
+
if (oidcCore === undefined) {
|
|
237
|
+
return undefined;
|
|
238
|
+
}
|
|
239
|
+
|
|
139
240
|
if (!oidcCore.isUserLoggedIn) {
|
|
140
241
|
return;
|
|
141
242
|
}
|
|
@@ -210,8 +311,30 @@ export function createOidcSpaApi<
|
|
|
210
311
|
};
|
|
211
312
|
}, []);
|
|
212
313
|
|
|
314
|
+
const [hasHydrated, setHasHydratedToTrue] = useReducer(
|
|
315
|
+
() => true,
|
|
316
|
+
assert_params !== undefined ? undefined : false
|
|
317
|
+
);
|
|
318
|
+
|
|
319
|
+
useEffect(() => {
|
|
320
|
+
if (hasHydrated === undefined) {
|
|
321
|
+
return;
|
|
322
|
+
}
|
|
323
|
+
setHasHydratedToTrue();
|
|
324
|
+
}, []);
|
|
325
|
+
|
|
326
|
+
if (!hasResolved || hasHydrated === false) {
|
|
327
|
+
return id<UseOidc.Oidc.InitNotCompleted>({
|
|
328
|
+
hasInitCompleted: false,
|
|
329
|
+
autoLogoutState: {
|
|
330
|
+
shouldDisplayWarning: false
|
|
331
|
+
}
|
|
332
|
+
});
|
|
333
|
+
}
|
|
334
|
+
|
|
213
335
|
if (!oidcCore.isUserLoggedIn) {
|
|
214
|
-
return id<
|
|
336
|
+
return id<UseOidc.Oidc.NotLoggedIn>({
|
|
337
|
+
hasInitCompleted: true,
|
|
215
338
|
isUserLoggedIn: false,
|
|
216
339
|
initializationError: oidcCore.initializationError,
|
|
217
340
|
issuerUri: oidcCore.params.issuerUri,
|
|
@@ -226,7 +349,8 @@ export function createOidcSpaApi<
|
|
|
226
349
|
});
|
|
227
350
|
}
|
|
228
351
|
|
|
229
|
-
return id<
|
|
352
|
+
return id<UseOidc.Oidc.LoggedIn<DecodedIdToken>>({
|
|
353
|
+
hasInitCompleted: true,
|
|
230
354
|
isUserLoggedIn: true,
|
|
231
355
|
get decodedIdToken() {
|
|
232
356
|
evtIsDecodedIdTokenUsed.current = true;
|
|
@@ -247,139 +371,8 @@ export function createOidcSpaApi<
|
|
|
247
371
|
});
|
|
248
372
|
}
|
|
249
373
|
|
|
250
|
-
const context_isFreeOfSsrHydrationConcern = createContext<boolean>(false);
|
|
251
|
-
|
|
252
|
-
function createOidcComponent<Props extends Record<string, unknown>>(params: {
|
|
253
|
-
assert?: "user logged in" | "user not logged in";
|
|
254
|
-
pendingComponent?: (props: NoInfer<Props>) => ReactNode;
|
|
255
|
-
component: (props: Props) => ReactNode;
|
|
256
|
-
}): ((props: Props) => ReactNode) & {
|
|
257
|
-
useOidc: () => CreateOidcComponent.Oidc<DecodedIdToken>;
|
|
258
|
-
} {
|
|
259
|
-
const {
|
|
260
|
-
assert: assert_params,
|
|
261
|
-
pendingComponent: PendingComponent,
|
|
262
|
-
component: Component
|
|
263
|
-
} = params;
|
|
264
|
-
|
|
265
|
-
const checkAssertion =
|
|
266
|
-
assert_params === undefined
|
|
267
|
-
? undefined
|
|
268
|
-
: (params: { isUserLoggedIn: boolean }): void => {
|
|
269
|
-
const { isUserLoggedIn } = params;
|
|
270
|
-
|
|
271
|
-
switch (assert_params) {
|
|
272
|
-
case "user not logged in":
|
|
273
|
-
if (isUserLoggedIn) {
|
|
274
|
-
throw new Error(
|
|
275
|
-
[
|
|
276
|
-
"oidc-spa: Asserted the user should not be logged in",
|
|
277
|
-
"but they are. Check your control flow."
|
|
278
|
-
].join(" ")
|
|
279
|
-
);
|
|
280
|
-
}
|
|
281
|
-
break;
|
|
282
|
-
case "user logged in":
|
|
283
|
-
if (!isUserLoggedIn) {
|
|
284
|
-
throw new Error(
|
|
285
|
-
[
|
|
286
|
-
"oidc-spa: Asserted the user should be logged in",
|
|
287
|
-
"but they arn't. Check your control flow."
|
|
288
|
-
].join(" ")
|
|
289
|
-
);
|
|
290
|
-
}
|
|
291
|
-
break;
|
|
292
|
-
default:
|
|
293
|
-
assert<Equals<typeof assert_params, never>>;
|
|
294
|
-
}
|
|
295
|
-
};
|
|
296
|
-
|
|
297
|
-
function ComponentWithOidc(props: Props) {
|
|
298
|
-
const renderFallback = () =>
|
|
299
|
-
PendingComponent === undefined ? null : <PendingComponent {...props} />;
|
|
300
|
-
|
|
301
|
-
if (!isBrowser) {
|
|
302
|
-
return renderFallback();
|
|
303
|
-
}
|
|
304
|
-
|
|
305
|
-
// NOTE: When the user assert that the user is logged in or not, they know.
|
|
306
|
-
// if they knows it means that they learned it somewhere so we are post SSR.
|
|
307
|
-
// Additionally, in autoLogin mode, the typedef don't allow this param to be provided.
|
|
308
|
-
const isFreeOfSsrHydrationConcern =
|
|
309
|
-
useContext(context_isFreeOfSsrHydrationConcern) || assert_params !== undefined;
|
|
310
|
-
|
|
311
|
-
const [oidcCore, setOidcCore] = useState<Oidc_core<DecodedIdToken> | undefined>(() => {
|
|
312
|
-
if (!isFreeOfSsrHydrationConcern) {
|
|
313
|
-
return undefined;
|
|
314
|
-
}
|
|
315
|
-
|
|
316
|
-
const { hasResolved, value: oidcCore } = dOidcCoreOrInitializationError.getState();
|
|
317
|
-
|
|
318
|
-
if (!hasResolved) {
|
|
319
|
-
return undefined;
|
|
320
|
-
}
|
|
321
|
-
|
|
322
|
-
if (oidcCore instanceof OidcInitializationError) {
|
|
323
|
-
return undefined;
|
|
324
|
-
}
|
|
325
|
-
|
|
326
|
-
checkAssertion?.({
|
|
327
|
-
isUserLoggedIn: oidcCore.isUserLoggedIn
|
|
328
|
-
});
|
|
329
|
-
|
|
330
|
-
return oidcCore;
|
|
331
|
-
});
|
|
332
|
-
|
|
333
|
-
useEffect(() => {
|
|
334
|
-
if (oidcCore !== undefined) {
|
|
335
|
-
return;
|
|
336
|
-
}
|
|
337
|
-
|
|
338
|
-
let isActive = true;
|
|
339
|
-
|
|
340
|
-
dOidcCoreOrInitializationError.pr.then(oidcCore => {
|
|
341
|
-
if (!isActive) {
|
|
342
|
-
return;
|
|
343
|
-
}
|
|
344
|
-
|
|
345
|
-
if (oidcCore instanceof OidcInitializationError) {
|
|
346
|
-
return;
|
|
347
|
-
}
|
|
348
|
-
|
|
349
|
-
checkAssertion?.({
|
|
350
|
-
isUserLoggedIn: oidcCore.isUserLoggedIn
|
|
351
|
-
});
|
|
352
|
-
|
|
353
|
-
setOidcCore(oidcCore);
|
|
354
|
-
});
|
|
355
|
-
|
|
356
|
-
return () => {
|
|
357
|
-
isActive = false;
|
|
358
|
-
};
|
|
359
|
-
}, []);
|
|
360
|
-
|
|
361
|
-
if (oidcCore === undefined) {
|
|
362
|
-
return PendingComponent === undefined ? null : <PendingComponent {...props} />;
|
|
363
|
-
}
|
|
364
|
-
|
|
365
|
-
return (
|
|
366
|
-
<context_isFreeOfSsrHydrationConcern.Provider value={true}>
|
|
367
|
-
<Component {...props} />
|
|
368
|
-
</context_isFreeOfSsrHydrationConcern.Provider>
|
|
369
|
-
);
|
|
370
|
-
}
|
|
371
|
-
|
|
372
|
-
ComponentWithOidc.displayName = `${
|
|
373
|
-
(Component as any).displayName ?? Component.name ?? "Component"
|
|
374
|
-
}WithOidc`;
|
|
375
|
-
|
|
376
|
-
ComponentWithOidc.useOidc = useOidc;
|
|
377
|
-
|
|
378
|
-
return ComponentWithOidc;
|
|
379
|
-
}
|
|
380
|
-
|
|
381
374
|
async function getOidc(params?: {
|
|
382
|
-
assert?: "user logged in" | "user not logged in";
|
|
375
|
+
assert?: "user logged in" | "user not logged in" | "init completed";
|
|
383
376
|
}): Promise<GetOidc.Oidc<DecodedIdToken>> {
|
|
384
377
|
if (!isBrowser) {
|
|
385
378
|
throw new UnifiedClientRetryForSsrLoadersError(
|
|
@@ -823,7 +816,9 @@ export function createOidcSpaApi<
|
|
|
823
816
|
|
|
824
817
|
function createFunctionMiddlewareServerFn(params?: {
|
|
825
818
|
assert?: "user logged in";
|
|
826
|
-
hasRequiredClaims?: (params: {
|
|
819
|
+
hasRequiredClaims?: (params: {
|
|
820
|
+
accessTokenClaims: AccessTokenClaims;
|
|
821
|
+
}) => MaybeAsync<boolean | undefined>;
|
|
827
822
|
}) {
|
|
828
823
|
return async (options: {
|
|
829
824
|
next: (options: { context: { oidc: OidcServerContext<AccessTokenClaims> } }) => any;
|
|
@@ -972,7 +967,9 @@ export function createOidcSpaApi<
|
|
|
972
967
|
|
|
973
968
|
function oidcRequestMiddleware(params?: {
|
|
974
969
|
assert?: "user logged in";
|
|
975
|
-
hasRequiredClaims?: (params: {
|
|
970
|
+
hasRequiredClaims?: (params: {
|
|
971
|
+
accessTokenClaims: AccessTokenClaims;
|
|
972
|
+
}) => MaybeAsync<boolean | undefined>;
|
|
976
973
|
}) {
|
|
977
974
|
return createMiddleware({ type: "request" }).server<{
|
|
978
975
|
oidc: OidcServerContext<AccessTokenClaims>;
|
|
@@ -981,7 +978,9 @@ export function createOidcSpaApi<
|
|
|
981
978
|
|
|
982
979
|
function oidcFnMiddleware(params?: {
|
|
983
980
|
assert?: "user logged in";
|
|
984
|
-
hasRequiredClaims?: (params: {
|
|
981
|
+
hasRequiredClaims?: (params: {
|
|
982
|
+
accessTokenClaims: AccessTokenClaims;
|
|
983
|
+
}) => MaybeAsync<boolean | undefined>;
|
|
985
984
|
}) {
|
|
986
985
|
return createMiddleware({ type: "function" })
|
|
987
986
|
.client(async ({ next }) => {
|
|
@@ -1014,7 +1013,7 @@ export function createOidcSpaApi<
|
|
|
1014
1013
|
|
|
1015
1014
|
// @ts-expect-error
|
|
1016
1015
|
return {
|
|
1017
|
-
|
|
1016
|
+
useOidc,
|
|
1018
1017
|
getOidc,
|
|
1019
1018
|
bootstrapOidc,
|
|
1020
1019
|
enforceLogin,
|
|
@@ -3,31 +3,36 @@ import type { Oidc as Oidc_core, OidcInitializationError } from "../../core";
|
|
|
3
3
|
import type { FunctionMiddlewareAfterServer, RequestMiddlewareAfterServer } from "@tanstack/react-start";
|
|
4
4
|
import type { GetterOrDirectValue } from "../../tools/GetterOrDirectValue";
|
|
5
5
|
import type { OidcMetadata } from "../../core/OidcMetadata";
|
|
6
|
+
import type { MaybeAsync } from "../../tools/MaybeAsync";
|
|
6
7
|
|
|
7
|
-
export type
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
assert?: Assert;
|
|
12
|
-
pendingComponent?: Assert extends undefined ? (props: NoInfer<Props>) => ReactNode : undefined;
|
|
13
|
-
component: (props: Props) => any;
|
|
14
|
-
}) => ((props: Props) => ReactNode) & {
|
|
15
|
-
useOidc: () => undefined extends Assert
|
|
16
|
-
? CreateOidcComponent.Oidc<DecodedIdToken>
|
|
17
|
-
: "user logged in" extends Assert
|
|
18
|
-
? CreateOidcComponent.Oidc.LoggedIn<DecodedIdToken>
|
|
19
|
-
: CreateOidcComponent.Oidc.NotLoggedIn;
|
|
8
|
+
export type UseOidc<DecodedIdToken> = {
|
|
9
|
+
(params?: { assert?: undefined }): UseOidc.Oidc<DecodedIdToken>;
|
|
10
|
+
(params: { assert: "user logged in" }): UseOidc.Oidc.LoggedIn<DecodedIdToken>;
|
|
11
|
+
(params: { assert: "user not logged in" }): UseOidc.Oidc.NotLoggedIn;
|
|
20
12
|
};
|
|
21
13
|
|
|
22
|
-
export namespace
|
|
23
|
-
export type WithAutoLogin<DecodedIdToken> =
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
}) => ((props: Props) => ReactNode) & {
|
|
27
|
-
useOidc: () => Oidc.LoggedIn<DecodedIdToken>;
|
|
28
|
-
};
|
|
14
|
+
export namespace UseOidc {
|
|
15
|
+
export type WithAutoLogin<DecodedIdToken> = (params?: {
|
|
16
|
+
assert: "init completed";
|
|
17
|
+
}) => Oidc.LoggedIn<DecodedIdToken>;
|
|
29
18
|
|
|
30
19
|
export type Oidc<DecodedIdToken> =
|
|
20
|
+
| (Oidc.InitNotCompleted & {
|
|
21
|
+
isUserLoggedIn?: never;
|
|
22
|
+
issuerUri?: never;
|
|
23
|
+
clientId?: never;
|
|
24
|
+
validRedirectUri?: never;
|
|
25
|
+
|
|
26
|
+
decodedIdToken?: never;
|
|
27
|
+
logout?: never;
|
|
28
|
+
renewTokens?: never;
|
|
29
|
+
goToAuthServer?: never;
|
|
30
|
+
backFromAuthServer?: never;
|
|
31
|
+
isNewBrowserSession?: never;
|
|
32
|
+
|
|
33
|
+
login?: never;
|
|
34
|
+
initializationError?: never;
|
|
35
|
+
})
|
|
31
36
|
| (Oidc.NotLoggedIn & {
|
|
32
37
|
decodedIdToken?: never;
|
|
33
38
|
logout?: never;
|
|
@@ -42,13 +47,19 @@ export namespace CreateOidcComponent {
|
|
|
42
47
|
});
|
|
43
48
|
|
|
44
49
|
export namespace Oidc {
|
|
45
|
-
type
|
|
50
|
+
export type InitNotCompleted = {
|
|
51
|
+
hasInitCompleted: false;
|
|
52
|
+
autoLogoutState: {
|
|
53
|
+
shouldDisplayWarning: false;
|
|
54
|
+
};
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
export type NotLoggedIn = {
|
|
58
|
+
hasInitCompleted: true;
|
|
59
|
+
isUserLoggedIn: false;
|
|
46
60
|
issuerUri: string;
|
|
47
61
|
clientId: string;
|
|
48
62
|
validRedirectUri: string;
|
|
49
|
-
};
|
|
50
|
-
|
|
51
|
-
export type NotLoggedIn = Common & {
|
|
52
63
|
login: (params?: {
|
|
53
64
|
extraQueryParams?: Record<string, string | undefined>;
|
|
54
65
|
redirectUrl?: string;
|
|
@@ -57,12 +68,15 @@ export namespace CreateOidcComponent {
|
|
|
57
68
|
autoLogoutState: {
|
|
58
69
|
shouldDisplayWarning: false;
|
|
59
70
|
};
|
|
60
|
-
isUserLoggedIn: false;
|
|
61
71
|
initializationError: OidcInitializationError | undefined;
|
|
62
72
|
};
|
|
63
73
|
|
|
64
|
-
export type LoggedIn<DecodedIdToken> =
|
|
74
|
+
export type LoggedIn<DecodedIdToken> = {
|
|
75
|
+
hasInitCompleted: true;
|
|
65
76
|
isUserLoggedIn: true;
|
|
77
|
+
issuerUri: string;
|
|
78
|
+
clientId: string;
|
|
79
|
+
validRedirectUri: string;
|
|
66
80
|
decodedIdToken: DecodedIdToken;
|
|
67
81
|
logout: Oidc_core.LoggedIn["logout"];
|
|
68
82
|
renewTokens: Oidc_core.LoggedIn["renewTokens"];
|
|
@@ -148,13 +162,17 @@ export namespace GetOidc {
|
|
|
148
162
|
export type OidcFnMiddleware<AccessTokenClaims> = {
|
|
149
163
|
(params?: {
|
|
150
164
|
assert?: undefined;
|
|
151
|
-
hasRequiredClaims?: (params: {
|
|
165
|
+
hasRequiredClaims?: (params: {
|
|
166
|
+
accessTokenClaims: AccessTokenClaims;
|
|
167
|
+
}) => MaybeAsync<boolean | undefined>;
|
|
152
168
|
}): OidcFnMiddleware.TanStackFnMiddleware<{
|
|
153
169
|
oidc: OidcServerContext<AccessTokenClaims>;
|
|
154
170
|
}>;
|
|
155
171
|
(params?: {
|
|
156
172
|
assert?: "user logged in";
|
|
157
|
-
hasRequiredClaims?: (params: {
|
|
173
|
+
hasRequiredClaims?: (params: {
|
|
174
|
+
accessTokenClaims: AccessTokenClaims;
|
|
175
|
+
}) => MaybeAsync<boolean | undefined>;
|
|
158
176
|
}): OidcFnMiddleware.TanStackFnMiddleware<{
|
|
159
177
|
oidc: OidcServerContext.LoggedIn<AccessTokenClaims>;
|
|
160
178
|
}>;
|
|
@@ -163,7 +181,9 @@ export type OidcFnMiddleware<AccessTokenClaims> = {
|
|
|
163
181
|
export namespace OidcFnMiddleware {
|
|
164
182
|
export type WithAutoLogin<AccessTokenClaims> = (params?: {
|
|
165
183
|
assert?: "user logged in";
|
|
166
|
-
hasRequiredClaims?: (params: {
|
|
184
|
+
hasRequiredClaims?: (params: {
|
|
185
|
+
accessTokenClaims: AccessTokenClaims;
|
|
186
|
+
}) => MaybeAsync<boolean | undefined>;
|
|
167
187
|
}) => TanStackFnMiddleware<{
|
|
168
188
|
oidc: OidcServerContext.LoggedIn<AccessTokenClaims>;
|
|
169
189
|
}>;
|
|
@@ -201,13 +221,17 @@ export namespace OidcServerContext {
|
|
|
201
221
|
export type OidcRequestMiddleware<AccessTokenClaims> = {
|
|
202
222
|
(params?: {
|
|
203
223
|
assert?: undefined;
|
|
204
|
-
hasRequiredClaims?: (params: {
|
|
224
|
+
hasRequiredClaims?: (params: {
|
|
225
|
+
accessTokenClaims: AccessTokenClaims;
|
|
226
|
+
}) => MaybeAsync<boolean | undefined>;
|
|
205
227
|
}): OidcRequestMiddleware.TanstackRequestMiddleware<{
|
|
206
228
|
oidc: OidcServerContext<AccessTokenClaims>;
|
|
207
229
|
}>;
|
|
208
230
|
(params?: {
|
|
209
231
|
assert?: "user logged in";
|
|
210
|
-
hasRequiredClaims?: (params: {
|
|
232
|
+
hasRequiredClaims?: (params: {
|
|
233
|
+
accessTokenClaims: AccessTokenClaims;
|
|
234
|
+
}) => MaybeAsync<boolean | undefined>;
|
|
211
235
|
}): OidcRequestMiddleware.TanstackRequestMiddleware<{
|
|
212
236
|
oidc: OidcServerContext.LoggedIn<AccessTokenClaims>;
|
|
213
237
|
}>;
|
|
@@ -216,7 +240,9 @@ export type OidcRequestMiddleware<AccessTokenClaims> = {
|
|
|
216
240
|
export namespace OidcRequestMiddleware {
|
|
217
241
|
export type WithAutoLogin<AccessTokenClaims> = (params?: {
|
|
218
242
|
assert?: "user logged in";
|
|
219
|
-
hasRequiredClaims?: (params: {
|
|
243
|
+
hasRequiredClaims?: (params: {
|
|
244
|
+
accessTokenClaims: AccessTokenClaims;
|
|
245
|
+
}) => MaybeAsync<boolean | undefined>;
|
|
220
246
|
}) => TanstackRequestMiddleware<{
|
|
221
247
|
oidc: OidcServerContext.LoggedIn<AccessTokenClaims>;
|
|
222
248
|
}>;
|
|
@@ -406,9 +432,7 @@ export type OidcSpaApi<AutoLogin, DecodedIdToken, AccessTokenClaims> = {
|
|
|
406
432
|
ParamsOfBootstrap<AutoLogin, DecodedIdToken, AccessTokenClaims>
|
|
407
433
|
>
|
|
408
434
|
) => void;
|
|
409
|
-
|
|
410
|
-
? CreateOidcComponent.WithAutoLogin<DecodedIdToken>
|
|
411
|
-
: CreateOidcComponent<DecodedIdToken>;
|
|
435
|
+
useOidc: AutoLogin extends true ? UseOidc.WithAutoLogin<DecodedIdToken> : UseOidc<DecodedIdToken>;
|
|
412
436
|
getOidc: AutoLogin extends true ? GetOidc.WithAutoLogin<DecodedIdToken> : GetOidc<DecodedIdToken>;
|
|
413
437
|
} & (AccessTokenClaims extends undefined
|
|
414
438
|
? {}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export type MaybeAsync<T> = Promise<T> | T;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export type MaybeAsync<T> = Promise<T> | T;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"MaybeAsync.js","sourceRoot":"","sources":["../src/tools/MaybeAsync.ts"],"names":[],"mappings":""}
|