diff options
author | Javier Calvarro Nelson <jacalvar@microsoft.com> | 2020-03-16 21:30:37 +0300 |
---|---|---|
committer | Javier Calvarro Nelson <jacalvar@microsoft.com> | 2020-03-16 21:30:37 +0300 |
commit | de84f191e773cb6abfb219ff24bbe3dacded7e03 (patch) | |
tree | cd099d973b0f7837d741bdb0daf4308e1d759f7a | |
parent | d2e87acd89c459b0537bf28dc0281a058c270557 (diff) |
Fix async initialization bugjaviercn/spa-fixes
-rw-r--r-- | src/Components/WebAssembly/WebAssembly.Authentication/src/Interop/AuthenticationService.ts | 19 | ||||
-rw-r--r-- | src/Components/WebAssembly/WebAssembly.Authentication/src/Interop/package.json | 1 |
2 files changed, 16 insertions, 4 deletions
diff --git a/src/Components/WebAssembly/WebAssembly.Authentication/src/Interop/AuthenticationService.ts b/src/Components/WebAssembly/WebAssembly.Authentication/src/Interop/AuthenticationService.ts index 1fe2d0a124..51df9e1f83 100644 --- a/src/Components/WebAssembly/WebAssembly.Authentication/src/Interop/AuthenticationService.ts +++ b/src/Components/WebAssembly/WebAssembly.Authentication/src/Interop/AuthenticationService.ts @@ -253,15 +253,26 @@ class OidcAuthorizeService implements AuthorizeService { export class AuthenticationService { static _infrastructureKey = 'Microsoft.AspNetCore.Components.WebAssembly.Authentication'; - static _initialized = false; + static _initialized : Promise<void>; static instance: OidcAuthorizeService; public static async init(settings: UserManagerSettings & AuthorizeServiceSettings) { + // Multiple initializations can start concurrently and we want to avoid that. + // In order to do so, we create an initialization promise and the first call to init + // tries to initialize the app and sets up a promise other calls can await on. if (!AuthenticationService._initialized) { - AuthenticationService._initialized = true; - const userManager = await this.createUserManager(settings); - AuthenticationService.instance = new OidcAuthorizeService(userManager); + this._initialized = new Promise(async (resolve, reject) => { + try { + const userManager = await this.createUserManager(settings); + AuthenticationService.instance = new OidcAuthorizeService(userManager); + resolve(); + } catch (e) { + reject(e); + } + }); } + + await this._initialized; } public static getUser() { diff --git a/src/Components/WebAssembly/WebAssembly.Authentication/src/Interop/package.json b/src/Components/WebAssembly/WebAssembly.Authentication/src/Interop/package.json index b0ae303717..dd40f447ed 100644 --- a/src/Components/WebAssembly/WebAssembly.Authentication/src/Interop/package.json +++ b/src/Components/WebAssembly/WebAssembly.Authentication/src/Interop/package.json @@ -1,4 +1,5 @@ { + "private": true, "scripts": { "build": "npm run build:release", "build:release": "webpack --mode production --env.production --env.configuration=Release", |