diff --git a/src/core/features/login/pages/credentials/credentials.ts b/src/core/features/login/pages/credentials/credentials.ts index 515b24671..cd2f1606d 100644 --- a/src/core/features/login/pages/credentials/credentials.ts +++ b/src/core/features/login/pages/credentials/credentials.ts @@ -80,10 +80,16 @@ export class CoreLoginCredentialsPage implements OnInit, OnDestroy { protected urlToOpen?: string; protected valueChangeSubscription?: Subscription; protected alwaysShowLoginFormObserver?: CoreEventObserver; + protected loginObserver?: CoreEventObserver; constructor( protected fb: FormBuilder, - ) {} + ) { + // Listen to LOGIN event to determine if login was successful, since the login can be done using QR, SSO, etc. + this.loginObserver = CoreEvents.on(CoreEvents.LOGIN, ({ siteId }) => { + this.siteId = siteId; + }); + } /** * @inheritdoc @@ -297,14 +303,12 @@ export class CoreLoginCredentialsPage implements OnInit, OnDestroy { try { const data = await CoreSites.getUserToken(siteUrl, username, password); - const id = await CoreSites.newSite(data.siteUrl, data.token, data.privateToken); + await CoreSites.newSite(data.siteUrl, data.token, data.privateToken); // Reset fields so the data is not in the view anymore. this.credForm.controls['username'].reset(); this.credForm.controls['password'].reset(); - this.siteId = id; - await CoreNavigator.navigateToSiteHome({ params: { urlToOpen: this.urlToOpen } }); } catch (error) { if (error instanceof CoreSiteError && CoreLoginHelper.isAppUnsupportedError(error)) { @@ -379,6 +383,7 @@ export class CoreLoginCredentialsPage implements OnInit, OnDestroy { ); this.valueChangeSubscription?.unsubscribe(); this.alwaysShowLoginFormObserver?.off(); + this.loginObserver?.off(); } } diff --git a/src/core/features/login/pages/reconnect/reconnect.ts b/src/core/features/login/pages/reconnect/reconnect.ts index b619dc568..972e9cd59 100644 --- a/src/core/features/login/pages/reconnect/reconnect.ts +++ b/src/core/features/login/pages/reconnect/reconnect.ts @@ -71,6 +71,7 @@ export class CoreLoginReconnectPage implements OnInit, OnDestroy { protected loginSuccessful = false; protected username = ''; protected alwaysShowLoginFormObserver?: CoreEventObserver; + protected loginObserver?: CoreEventObserver; constructor( protected fb: FormBuilder, @@ -81,6 +82,11 @@ export class CoreLoginReconnectPage implements OnInit, OnDestroy { this.credForm = fb.group({ password: ['', Validators.required], }); + + // Listen to LOGIN event to determine if login was successful, since the login can be done using QR, biometric, etc. + this.loginObserver = CoreEvents.on(CoreEvents.LOGIN, () => { + this.loginSuccessful = true; + }); } /** @@ -151,10 +157,12 @@ export class CoreLoginReconnectPage implements OnInit, OnDestroy { { config: this.siteConfig, loginSuccessful: this.loginSuccessful, + siteId: this.siteId, }, this.siteId, ); this.alwaysShowLoginFormObserver?.off(); + this.loginObserver?.off(); } /** @@ -190,7 +198,7 @@ export class CoreLoginReconnectPage implements OnInit, OnDestroy { if (!this.eventThrown && !this.viewLeft) { this.eventThrown = true; - CoreEvents.trigger(CoreEvents.LOGIN_SITE_CHECKED, { config: this.siteConfig }); + CoreEvents.trigger(CoreEvents.LOGIN_SITE_CHECKED, { config: this.siteConfig, siteId: this.siteId }); } this.isBrowserSSO = CoreLoginHelper.isSSOLoginNeeded(this.siteConfig.typeoflogin); @@ -261,8 +269,6 @@ export class CoreLoginReconnectPage implements OnInit, OnDestroy { this.credForm.controls['password'].reset(); // Go to the site initial page. - this.loginSuccessful = true; - await CoreNavigator.navigateToSiteHome({ params: this.redirectData, }); diff --git a/src/core/features/styles/services/styles.ts b/src/core/features/styles/services/styles.ts index 67ea5f19a..a62ee264e 100644 --- a/src/core/features/styles/services/styles.ts +++ b/src/core/features/styles/services/styles.ts @@ -148,8 +148,15 @@ export class CoreStylesService { this.removeSite(site.getId()); }); - // Load temporary styles when site config is checked in login. + // Load temporary styles when site config is checked in login/reconnect. CoreEvents.on(CoreEvents.LOGIN_SITE_CHECKED, (data) => { + if (data.siteId) { + // Reconnecting to a site, enable the site styles. + this.enableSiteStyles(data.siteId); + + return; + } + this.loadTmpStyles(data.config).catch((error) => { this.logger.error('Error loading tmp styles', error); }); @@ -163,8 +170,9 @@ export class CoreStylesService { return; } - // The tmp styles are from a site that wasn't added in the end. Just remove them. + // User didn't access the site, unload tmp styles and site styles if any. this.unloadTmpStyles(); + this.clear(); }); } diff --git a/src/core/services/sites.ts b/src/core/services/sites.ts index 0cbb0b27d..21d7901f3 100644 --- a/src/core/services/sites.ts +++ b/src/core/services/sites.ts @@ -1443,7 +1443,7 @@ export class CoreSitesProvider { async login(siteId: string): Promise { await CoreConfig.set(CORE_SITE_CURRENT_SITE_ID_CONFIG, siteId); - CoreEvents.trigger(CoreEvents.LOGIN, {}, siteId); + CoreEvents.trigger(CoreEvents.LOGIN, { siteId }, siteId); } /** diff --git a/src/core/services/tests/sites.test.ts b/src/core/services/tests/sites.test.ts index 7a274b4f2..68e477405 100644 --- a/src/core/services/tests/sites.test.ts +++ b/src/core/services/tests/sites.test.ts @@ -72,7 +72,7 @@ describe('CoreSitesProvider', () => { getCurrentSiteId: () => '42', }); - CoreEvents.trigger(CoreEvents.LOGIN, {}, '42'); + CoreEvents.trigger(CoreEvents.LOGIN, { siteId: '42' }, '42'); // Wait the event to be processed. await CoreWait.nextTick(); diff --git a/src/core/singletons/events.ts b/src/core/singletons/events.ts index cc8eab779..53ec4d871 100644 --- a/src/core/singletons/events.ts +++ b/src/core/singletons/events.ts @@ -56,6 +56,7 @@ export interface CoreEventsData { [CoreEvents.IAB_LOAD_START]: InAppBrowserEvent; [CoreEvents.IAB_LOAD_STOP]: InAppBrowserEvent; [CoreEvents.IAB_MESSAGE]: Record; + [CoreEvents.LOGIN]: { siteId: string }; [CoreEvents.LOGIN_SITE_CHECKED]: CoreEventLoginSiteCheckedData; [CoreEvents.LOGIN_SITE_UNCHECKED]: CoreEventLoginSiteUncheckedData; [CoreEvents.SEND_ON_ENTER_CHANGED]: CoreEventSendOnEnterChangedData; @@ -428,6 +429,7 @@ export type CoreEventActivityDataSentData = { */ export type CoreEventLoginSiteCheckedData = { config: CoreSitePublicConfigResponse; + siteId?: string; }; /** @@ -436,6 +438,7 @@ export type CoreEventLoginSiteCheckedData = { export type CoreEventLoginSiteUncheckedData = { config?: CoreSitePublicConfigResponse; loginSuccessful: boolean; + siteId?: string; }; /**