From 94444fbc96c49e27eda3fbf7eec3bb222832acf3 Mon Sep 17 00:00:00 2001 From: Dani Palou Date: Wed, 20 Jul 2022 12:03:30 +0200 Subject: [PATCH] MOBILE-4034 login: Close IAB after user changes password --- .../pages/change-password/change-password.ts | 43 ++++++++++++++++++- 1 file changed, 41 insertions(+), 2 deletions(-) diff --git a/src/core/features/login/pages/change-password/change-password.ts b/src/core/features/login/pages/change-password/change-password.ts index 1c0428e4e..697e5d939 100644 --- a/src/core/features/login/pages/change-password/change-password.ts +++ b/src/core/features/login/pages/change-password/change-password.ts @@ -12,13 +12,15 @@ // See the License for the specific language governing permissions and // limitations under the License. -import { Component } from '@angular/core'; +import { Component, OnDestroy } from '@angular/core'; import { CoreSites } from '@services/sites'; import { CoreDomUtils } from '@services/utils/dom'; import { CoreLoginHelper } from '@features/login/services/login-helper'; import { Translate } from '@singletons'; import { CoreNavigator } from '@services/navigator'; +import { CoreEventObserver, CoreEvents } from '@singletons/events'; +import { CoreUtils } from '@services/utils/utils'; /** * Page that shows instructions to change the password. @@ -27,11 +29,14 @@ import { CoreNavigator } from '@services/navigator'; selector: 'page-core-login-change-password', templateUrl: 'change-password.html', }) -export class CoreLoginChangePasswordPage { +export class CoreLoginChangePasswordPage implements OnDestroy { changingPassword = false; logoutLabel: string; + protected urlLoadedObserver?: CoreEventObserver; + protected browserClosedObserver?: CoreEventObserver; + constructor() { this.logoutLabel = CoreLoginHelper.getLogoutLabel(); } @@ -57,6 +62,7 @@ export class CoreLoginChangePasswordPage { true, ); this.changingPassword = true; + this.detectPasswordChanged(); } /** @@ -75,4 +81,37 @@ export class CoreLoginChangePasswordPage { this.changingPassword = false; } + /** + * Try to detect if the user changed password in browser. + */ + detectPasswordChanged(): void { + if (this.urlLoadedObserver) { + // Already listening (shouldn't happen). + return; + } + + this.urlLoadedObserver = CoreEvents.on(CoreEvents.IAB_LOAD_START, (event) => { + if (event.url.match(/\/login\/change_password\.php.*return=1/)) { + // Password should have changed. + CoreUtils.closeInAppBrowser(); + this.login(); + } + }); + + this.browserClosedObserver = CoreEvents.on(CoreEvents.IAB_EXIT, () => { + this.urlLoadedObserver?.off(); + this.browserClosedObserver?.off(); + delete this.urlLoadedObserver; + delete this.browserClosedObserver; + }); + } + + /** + * @inheritdoc + */ + ngOnDestroy(): void { + this.urlLoadedObserver?.off(); + this.browserClosedObserver?.off(); + } + }