From c8c7307abf588ed78442d40c558a55b2c2a15c28 Mon Sep 17 00:00:00 2001 From: Alexey Date: Sat, 27 May 2023 18:50:06 +0400 Subject: [PATCH] CB-3135 fix: auth dialog appearance (#1697) --- .../src/AuthenticationService.ts | 26 +++++++++++----- .../src/Dialog/AuthDialogService.ts | 31 ++++++++++++++++--- 2 files changed, 46 insertions(+), 11 deletions(-) diff --git a/webapp/packages/plugin-authentication/src/AuthenticationService.ts b/webapp/packages/plugin-authentication/src/AuthenticationService.ts index 36656ae470..7d35ebcd66 100644 --- a/webapp/packages/plugin-authentication/src/AuthenticationService.ts +++ b/webapp/packages/plugin-authentication/src/AuthenticationService.ts @@ -126,14 +126,14 @@ export class AuthenticationService extends Bootstrap { } private async auth(persistent: boolean, options: IAuthOptions) { - const contexts = await this.onLogin.execute('before'); - - if (ExecutorInterrupter.isInterrupted(contexts)) { + if (this.authPromise) { + await this.waitAuth(); return; } - if (this.authPromise) { - await this.authPromise; + const contexts = await this.onLogin.execute('before'); + + if (ExecutorInterrupter.isInterrupted(contexts)) { return; } @@ -170,6 +170,8 @@ export class AuthenticationService extends Bootstrap { } private async requireAuthentication() { + await this.waitAuth(); + const authNeeded = await this.appAuthService.isAuthNeeded(); if (!authNeeded) { return; @@ -188,6 +190,8 @@ export class AuthenticationService extends Bootstrap { this.screenService.routeChange.addHandler(() => this.requireAuthentication()); this.administrationScreenService.ensurePermissions.addHandler(async () => { + await this.waitAuth(); + const userInfo = await this.userInfoResource.load(); if (userInfo) { return; @@ -209,14 +213,16 @@ export class AuthenticationService extends Bootstrap { if (isAutoLoginSessionAction(data)) { const user = await this.userInfoResource.finishFederatedAuthentication(data['auth-id'], false); - if (user && this.authPromise) { - this.authDialogService.closeLoginForm(this.authPromise); + if (user) { + this.authDialogService.closeLoginForm(); } action.process(); } } private readonly requestAuthProviderHandler: IExecutorHandler = async (data, contexts) => { + await this.waitAuth(); + if (data.providerId === AUTH_PROVIDER_LOCAL_ID) { const provider = contexts.getContext(AuthProviderContext); provider.auth(); @@ -239,4 +245,10 @@ export class AuthenticationService extends Bootstrap { provider.auth(); } }; + + private async waitAuth() { + try { + await this.authPromise; + } catch {} + } } diff --git a/webapp/packages/plugin-authentication/src/Dialog/AuthDialogService.ts b/webapp/packages/plugin-authentication/src/Dialog/AuthDialogService.ts index 5a5bbab328..701eb18a28 100644 --- a/webapp/packages/plugin-authentication/src/Dialog/AuthDialogService.ts +++ b/webapp/packages/plugin-authentication/src/Dialog/AuthDialogService.ts @@ -14,9 +14,19 @@ import { AuthDialog } from './AuthDialog'; @injectable() export class AuthDialogService { + get isPersistent(): boolean { + return this.persistent; + } + + private persistent: boolean; + private dialog: Promise | null; + constructor( private readonly commonDialogService: CommonDialogService - ) { } + ) { + this.persistent = false; + this.dialog = null; + } showLoginForm( persistent = false, @@ -24,10 +34,23 @@ export class AuthDialogService { providerId: null, } ): Promise { - return this.commonDialogService.open(AuthDialog, options, { persistent }); + if (this.dialog) { + return this.dialog; + } + + this.persistent = persistent; + this.dialog = this.commonDialogService.open(AuthDialog, options, { persistent }); + this.dialog.finally(() => { + this.dialog = null; + this.persistent = false; + }); + + return this.dialog; } - closeLoginForm(promise: Promise): void { - this.commonDialogService.rejectDialog(promise); + closeLoginForm(): void { + if (this.dialog) { + this.commonDialogService.rejectDialog(this.dialog); + } } }