From 3dfbca351728537d9f34a135210bacab0cf35315 Mon Sep 17 00:00:00 2001 From: Andrey Skladchikov Date: Fri, 30 Aug 2024 09:23:47 +0200 Subject: [PATCH] Put page reload in timeout for USER_CHANGED event to let all other events to be called. JT-83975 related --- src/auth/auth.test.ts | 10 ++++------ src/auth/auth__core.ts | 5 ++++- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/src/auth/auth.test.ts b/src/auth/auth.test.ts index 78f7dfbf8ac..cc1ba3c86b6 100644 --- a/src/auth/auth.test.ts +++ b/src/auth/auth.test.ts @@ -113,15 +113,18 @@ describe('Auth', () => { }); it('should perform redirect on userChange by default', () => { + const clock = sandbox.useFakeTimers({toFake: ['setTimeout']}); sandbox.stub(Auth.prototype, '_redirectCurrentPage'); const auth = new Auth({serverUri: ''}); auth.listeners.trigger(USER_CHANGED_EVENT); + clock.tick(0); Auth.prototype._redirectCurrentPage.should.have.been.called; }); it('should not perform redirect on userChange when reloadOnUserChange is false', () => { + const clock = sandbox.useFakeTimers({toFake: ['setTimeout']}); sandbox.stub(Auth.prototype, '_redirectCurrentPage'); const auth = new Auth({ @@ -129,6 +132,7 @@ describe('Auth', () => { serverUri: '' }); auth.listeners.trigger(USER_CHANGED_EVENT); + clock.tick(0); Auth.prototype._redirectCurrentPage.should.not.been.called; }); @@ -528,12 +532,6 @@ describe('Auth', () => { 'token'.should.be.equal(accessToken); }); - it('should reload page if user change was applied', () => { - auth.user = {id: 'initUser'} as AuthUser; - auth.listeners.trigger(USER_CHANGED_EVENT); - Auth.prototype._redirectCurrentPage.should.have.been.calledWith(window.location.href); - }); - it('should redirect current page if get token in iframe fails', async () => { auth.config.embeddedLogin = false; if (auth._backgroundFlow != null) { diff --git a/src/auth/auth__core.ts b/src/auth/auth__core.ts index 8c7ffa92a5d..1b267938dd5 100644 --- a/src/auth/auth__core.ts +++ b/src/auth/auth__core.ts @@ -292,7 +292,10 @@ export default class Auth implements HTTPAuth { } if (this.config.reloadOnUserChange) { - this.addListener(USER_CHANGED_EVENT, () => this._reloadCurrentPage()); + this.addListener(USER_CHANGED_EVENT, () => { + // Timeout is needed to ensure all other listeners triggered before stopping current page + setTimeout(() => this._reloadCurrentPage()); + }); } this.addListener(LOGOUT_POSTPONED_EVENT, () => this._setPostponed(true));