-
Notifications
You must be signed in to change notification settings - Fork 26
#169 #170 #32 #171
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
#169 #170 #32 #171
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,6 +7,7 @@ import { | |
| import { type OidcMetadata, fetchOidcMetadata } from "./OidcMetadata"; | ||
| import { assert, type Equals } from "../tools/tsafe/assert"; | ||
| import { id } from "../tools/tsafe/id"; | ||
| import { Reflect } from "../tools/tsafe/Reflect"; | ||
| import { Deferred } from "../tools/Deferred"; | ||
| import { createEvtIsUserActive } from "./evtIsUserActive"; | ||
| import { createStartCountdown } from "../tools/startCountdown"; | ||
|
|
@@ -266,14 +267,51 @@ export type ParamsOfCreateOidc< | |
| disableDPoP?: true; | ||
| }; | ||
|
|
||
| const globalContext = { | ||
| prOidcByConfigId: new Map<string, Promise<Oidc<any>>>(), | ||
| hasLogoutBeenCalled: id<boolean>(false), | ||
| dExports_earlyInit: new Deferred<Exports_earlyInit>(), | ||
| dExports_tokenSubstitution: new Deferred<Exports_tokenSubstitution>(), | ||
| dExports_DPoP: new Deferred<Exports_DPoP>() | ||
| type GlobalContextActual = { | ||
| prOidcByConfigId: Map<string, Promise<Oidc<any>>>; | ||
| hasLogoutBeenCalled: boolean; | ||
| exports_earlyInit: Exports_earlyInit; | ||
| exports_tokenSubstitution: Exports_tokenSubstitution | undefined; | ||
| exports_DPoP: Exports_DPoP | undefined; | ||
| }; | ||
|
|
||
| declare global { | ||
| interface Window { | ||
| "__oidc-spa:globalContext:createOidc": { | ||
| dEarlyInitResolved: Deferred<void>; | ||
| actual_exposedForMicroFrontendSetup: | ||
| | GlobalContextActual | ||
| | "not a micro frontend setup" | ||
| | undefined; | ||
| }; | ||
| } | ||
| } | ||
|
|
||
| window["__oidc-spa:globalContext:createOidc"] ??= { | ||
| dEarlyInitResolved: new Deferred(), | ||
| actual_exposedForMicroFrontendSetup: undefined | ||
| }; | ||
|
|
||
| const globalContext = window["__oidc-spa:globalContext:createOidc"]; | ||
|
|
||
| const globalContext_actual_moduleScoped: GlobalContextActual = { | ||
| prOidcByConfigId: new Map(), | ||
| hasLogoutBeenCalled: false, | ||
| exports_earlyInit: Reflect<Exports_earlyInit>(), | ||
| exports_tokenSubstitution: undefined, | ||
| exports_DPoP: undefined | ||
| }; | ||
|
|
||
| function getGlobalContextActual(): GlobalContextActual { | ||
| assert(globalContext.actual_exposedForMicroFrontendSetup !== undefined); | ||
|
|
||
| if (globalContext.actual_exposedForMicroFrontendSetup === "not a micro frontend setup") { | ||
| return globalContext_actual_moduleScoped; | ||
| } | ||
|
|
||
| return globalContext.actual_exposedForMicroFrontendSetup; | ||
| } | ||
|
|
||
| export type Exports_earlyInit = | ||
| | { shouldLoadApp: false } | ||
| | { | ||
|
|
@@ -287,8 +325,23 @@ export type Exports_earlyInit = | |
| sessionRestorationMethod: "iframe" | "full page redirect" | "auto" | undefined; | ||
| }; | ||
|
|
||
| export function registerExports_earlyInit(exports: Exports_earlyInit): void { | ||
| globalContext.dExports_earlyInit.resolve(exports); | ||
| export function registerExports_earlyInit(params: { | ||
| exports: Exports_earlyInit; | ||
| isMicroFrontendSetup: boolean; | ||
| }): void { | ||
| const { exports, isMicroFrontendSetup } = params; | ||
|
|
||
| if (globalContext.actual_exposedForMicroFrontendSetup !== undefined) { | ||
| throw new Error("oidc-spa: Wrong micro frontend setup"); | ||
| } | ||
|
|
||
| globalContext.actual_exposedForMicroFrontendSetup = isMicroFrontendSetup | ||
| ? globalContext_actual_moduleScoped | ||
| : "not a micro frontend setup"; | ||
|
|
||
| globalContext_actual_moduleScoped.exports_earlyInit = exports; | ||
|
|
||
| globalContext.dEarlyInitResolved.resolve(); | ||
| } | ||
|
Comment on lines
+328
to
345
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Allow repeated Every 💡 Minimal fix export function registerExports_earlyInit(params: {
exports: Exports_earlyInit;
isMicroFrontendSetup: boolean;
}): void {
const { exports, isMicroFrontendSetup } = params;
+ const existing = globalContext.actual_exposedForMicroFrontendSetup;
- if (globalContext.actual_exposedForMicroFrontendSetup !== undefined) {
- throw new Error("oidc-spa: Wrong micro frontend setup");
+ if (existing !== undefined) {
+ if (existing !== "not a micro frontend setup" && isMicroFrontendSetup) {
+ return;
+ }
+
+ throw new Error("oidc-spa: Wrong micro frontend setup");
}
globalContext.actual_exposedForMicroFrontendSetup = isMicroFrontendSetup
? globalContext_actual_moduleScoped
: "not a micro frontend setup";
globalContext_actual_moduleScoped.exports_earlyInit = exports;
globalContext.dEarlyInitResolved.resolve();
}🤖 Prompt for AI Agents |
||
|
|
||
| export type Exports_tokenSubstitution = { | ||
|
|
@@ -307,7 +360,8 @@ export namespace Exports_tokenSubstitution { | |
| } | ||
|
|
||
| export function registerExports_tokenSubstitution(exports: Exports_tokenSubstitution): void { | ||
| globalContext.dExports_tokenSubstitution.resolve(exports); | ||
| assert(!globalContext_actual_moduleScoped.exports_earlyInit); | ||
| globalContext_actual_moduleScoped.exports_tokenSubstitution = exports; | ||
| } | ||
|
|
||
| export type Exports_DPoP = { | ||
|
|
@@ -339,7 +393,8 @@ export namespace Exports_DPoP { | |
| } | ||
|
|
||
| export function registerExports_DPoP(exports: Exports_DPoP): void { | ||
| globalContext.dExports_DPoP.resolve(exports); | ||
| assert(!globalContext_actual_moduleScoped.exports_earlyInit); | ||
| globalContext_actual_moduleScoped.exports_DPoP = exports; | ||
| } | ||
|
|
||
| /** @see: https://docs.oidc-spa.dev/v/v10/usage */ | ||
|
|
@@ -384,7 +439,30 @@ export async function createOidc< | |
|
|
||
| const configId = getConfigId({ issuerUri, clientId }); | ||
|
|
||
| const { prOidcByConfigId } = globalContext; | ||
| wait_early_init: { | ||
| const { dEarlyInitResolved } = globalContext; | ||
|
|
||
| if (dEarlyInitResolved.getState().hasResolved) { | ||
| break wait_early_init; | ||
| } | ||
|
|
||
| const timer = window.setTimeout(() => { | ||
| console.warn( | ||
| [ | ||
| "oidc-spa: Setup error.", | ||
| "oidcEarlyInit() wasn't called.", | ||
| "This is supposed to be handled by the oidc-spa Vite plugin", | ||
| "or manually in other environments." | ||
| ].join(" ") | ||
| ); | ||
| }, 3_000); | ||
|
|
||
| await dEarlyInitResolved.pr; | ||
|
|
||
| window.clearTimeout(timer); | ||
| } | ||
|
|
||
| const { prOidcByConfigId } = getGlobalContextActual(); | ||
|
|
||
| use_previous_instance: { | ||
| const prOidc = prOidcByConfigId.get(configId); | ||
|
|
@@ -452,24 +530,7 @@ export async function createOidc_nonMemoized< | |
| BASE_URL: BASE_URL_params | ||
| } = params; | ||
|
|
||
| const exports_earlyInit = await (async () => { | ||
| const timer = window.setTimeout(() => { | ||
| console.warn( | ||
| [ | ||
| "oidc-spa: Setup error.", | ||
| "oidcEarlyInit() wasn't called.", | ||
| "This is supposed to be handled by the oidc-spa Vite plugin", | ||
| "or manually in other environments." | ||
| ].join(" ") | ||
| ); | ||
| }, 3_000); | ||
|
|
||
| const exports_earlyInit = await globalContext.dExports_earlyInit.pr; | ||
|
|
||
| window.clearTimeout(timer); | ||
|
|
||
| return exports_earlyInit; | ||
| })(); | ||
| const { exports_earlyInit } = getGlobalContextActual(); | ||
|
|
||
| if (!exports_earlyInit.shouldLoadApp) { | ||
| return new Promise<never>(() => {}); | ||
|
|
@@ -484,9 +545,9 @@ export async function createOidc_nonMemoized< | |
| const sessionRestorationMethod = | ||
| sessionRestorationMethod_params ?? sessionRestorationMethod_earlyInit ?? "auto"; | ||
|
|
||
| const { value: exports_tokenSubstitution } = globalContext.dExports_tokenSubstitution.getState(); | ||
| const { exports_tokenSubstitution } = getGlobalContextActual(); | ||
|
|
||
| const { value: exports_DPoP } = globalContext.dExports_DPoP.getState(); | ||
| const { exports_DPoP } = getGlobalContextActual(); | ||
|
|
||
| const scopes = Array.from(new Set(["openid", ...(params.scopes ?? ["profile"])])); | ||
|
|
||
|
|
@@ -1532,12 +1593,12 @@ export async function createOidc_nonMemoized< | |
| }, | ||
| getDecodedIdToken: () => currentTokens.decodedIdToken, | ||
| logout: async params => { | ||
| if (globalContext.hasLogoutBeenCalled) { | ||
| if (getGlobalContextActual().hasLogoutBeenCalled) { | ||
| log?.("logout() has already been called, ignoring the call"); | ||
| return new Promise<never>(() => {}); | ||
| } | ||
|
|
||
| globalContext.hasLogoutBeenCalled = true; | ||
| getGlobalContextActual().hasLogoutBeenCalled = true; | ||
|
|
||
| const rootRelativePostLogoutRedirectUrl: string = (() => { | ||
| switch (params.redirectTo) { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,16 +4,26 @@ import { assert, type Equals } from "../tools/tsafe/assert"; | |
| import { noUndefined } from "../tools/tsafe/noUndefined"; | ||
| import type { StateData } from "./StateData"; | ||
| import type { NonPostableEvt } from "../tools/Evt"; | ||
| import { createStatefulEvt } from "../tools/StatefulEvt"; | ||
| import { createStatefulEvt, StatefulEvt } from "../tools/StatefulEvt"; | ||
| import { Deferred } from "../tools/Deferred"; | ||
| import { addOrUpdateSearchParam, getAllSearchParams } from "../tools/urlSearchParams"; | ||
| import { getIsOnline } from "../tools/getIsOnline"; | ||
| import { setStateDataCookieIfEnabled } from "./StateDataCookie"; | ||
|
|
||
| const globalContext = { | ||
| declare global { | ||
| interface Window { | ||
| "__oidc-spa:globalContext:loginOrGoToAuthServer": { | ||
| evtHasLoginBeenCalled: StatefulEvt<boolean>; | ||
| }; | ||
| } | ||
| } | ||
|
|
||
| window["__oidc-spa:globalContext:loginOrGoToAuthServer"] ??= { | ||
| evtHasLoginBeenCalled: createStatefulEvt(() => false) | ||
| }; | ||
|
|
||
| const globalContext = window["__oidc-spa:globalContext:loginOrGoToAuthServer"]; | ||
|
Comment on lines
+21
to
+25
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Window access at module evaluation time breaks SSR. Same issue as the other files in this PR - immediate window access at import time causes errors in SSR/Node.js environments. 🤖 Prompt for AI Agents |
||
|
|
||
| type Params = Params.Login | Params.GoToAuthServer; | ||
|
|
||
| namespace Params { | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.