MOBILE-4572 login: Wait for DB write before launching SSO

main
Dani Palou 2024-04-10 16:06:11 +02:00
parent a54d8e7b64
commit 02d3ccfdc0
3 changed files with 45 additions and 27 deletions

View File

@ -92,8 +92,8 @@ export class CoreLoginMethodsComponent implements OnInit {
* *
* @param provider The provider that was clicked. * @param provider The provider that was clicked.
*/ */
oauthClicked(provider: CoreSiteIdentityProvider): void { async oauthClicked(provider: CoreSiteIdentityProvider): Promise<void> {
const result = CoreLoginHelper.openBrowserForOAuthLogin( const result = await CoreLoginHelper.openBrowserForOAuthLogin(
this.siteUrl, this.siteUrl,
provider, provider,
this.siteConfig?.launchurl, this.siteConfig?.launchurl,

View File

@ -318,7 +318,7 @@ export class CoreLoginEmailSignupPage implements OnInit {
}; };
if (this.siteConfig?.launchurl) { if (this.siteConfig?.launchurl) {
params.redirect = CoreLoginHelper.prepareForSSOLogin(this.site.getURL(), undefined, this.siteConfig.launchurl); params.redirect = await CoreLoginHelper.prepareForSSOLogin(this.site.getURL(), undefined, this.siteConfig.launchurl);
} }
// Get the recaptcha response (if needed). // Get the recaptcha response (if needed).

View File

@ -613,12 +613,12 @@ export class CoreLoginHelperProvider {
* @param redirectData Data of the path/url to open once authenticated. If not defined, site initial page. * @param redirectData Data of the path/url to open once authenticated. If not defined, site initial page.
* @returns True if success, false if error. * @returns True if success, false if error.
*/ */
openBrowserForOAuthLogin( async openBrowserForOAuthLogin(
siteUrl: string, siteUrl: string,
provider: CoreSiteIdentityProvider, provider: CoreSiteIdentityProvider,
launchUrl?: string, launchUrl?: string,
redirectData?: CoreRedirectPayload, redirectData?: CoreRedirectPayload,
): boolean { ): Promise<boolean> {
launchUrl = launchUrl || siteUrl + '/admin/tool/mobile/launch.php'; launchUrl = launchUrl || siteUrl + '/admin/tool/mobile/launch.php';
this.logger.debug('openBrowserForOAuthLogin launchUrl:', launchUrl); this.logger.debug('openBrowserForOAuthLogin launchUrl:', launchUrl);
@ -633,15 +633,25 @@ export class CoreLoginHelperProvider {
return false; return false;
} }
const loginUrl = this.prepareForSSOLogin(siteUrl, undefined, launchUrl, redirectData, { const modal = await CoreDomUtils.showModalLoading();
oauthsso: params.id,
});
// Always open it in browser because the user might have the session stored in there. try {
CoreUtils.openInBrowser(loginUrl, { showBrowserWarning: false }); const loginUrl = await this.prepareForSSOLogin(siteUrl, undefined, launchUrl, redirectData, {
CoreApp.closeApp(); oauthsso: params.id,
});
return true; // Always open it in browser because the user might have the session stored in there.
CoreUtils.openInBrowser(loginUrl, { showBrowserWarning: false });
CoreApp.closeApp();
return true;
} catch (error) {
CoreDomUtils.showErrorModalDefault(error, 'Error opening browser');
} finally {
modal.dismiss();
}
return false;
} }
/** /**
@ -653,25 +663,33 @@ export class CoreLoginHelperProvider {
* @param launchUrl The URL to open for SSO. If not defined, default tool mobile launch URL will be used. * @param launchUrl The URL to open for SSO. If not defined, default tool mobile launch URL will be used.
* @param redirectData Data of the path/url to open once authenticated. If not defined, site initial page. * @param redirectData Data of the path/url to open once authenticated. If not defined, site initial page.
*/ */
openBrowserForSSOLogin( async openBrowserForSSOLogin(
siteUrl: string, siteUrl: string,
typeOfLogin: TypeOfLogin, typeOfLogin: TypeOfLogin,
service?: string, service?: string,
launchUrl?: string, launchUrl?: string,
redirectData?: CoreRedirectPayload, redirectData?: CoreRedirectPayload,
): void { ): Promise<void> {
const loginUrl = this.prepareForSSOLogin(siteUrl, service, launchUrl, redirectData); const modal = await CoreDomUtils.showModalLoading();
this.logger.debug('openBrowserForSSOLogin loginUrl:', loginUrl); try {
const loginUrl = await this.prepareForSSOLogin(siteUrl, service, launchUrl, redirectData);
if (this.isSSOEmbeddedBrowser(typeOfLogin)) { this.logger.debug('openBrowserForSSOLogin loginUrl:', loginUrl);
CoreUtils.openInApp(loginUrl, {
clearsessioncache: 'yes', // Clear the session cache to allow for multiple logins. if (this.isSSOEmbeddedBrowser(typeOfLogin)) {
closebuttoncaption: Translate.instant('core.login.cancel'), CoreUtils.openInApp(loginUrl, {
}); clearsessioncache: 'yes', // Clear the session cache to allow for multiple logins.
} else { closebuttoncaption: Translate.instant('core.login.cancel'),
CoreUtils.openInBrowser(loginUrl, { showBrowserWarning: false }); });
CoreApp.closeApp(); } else {
CoreUtils.openInBrowser(loginUrl, { showBrowserWarning: false });
CoreApp.closeApp();
}
} catch (error) {
CoreDomUtils.showErrorModalDefault(error, 'Error opening browser');
} finally {
modal.dismiss();
} }
} }
@ -779,13 +797,13 @@ export class CoreLoginHelperProvider {
* @param urlParams Other params to add to the URL. * @param urlParams Other params to add to the URL.
* @returns Login Url. * @returns Login Url.
*/ */
prepareForSSOLogin( async prepareForSSOLogin(
siteUrl: string, siteUrl: string,
service?: string, service?: string,
launchUrl?: string, launchUrl?: string,
redirectData: CoreRedirectPayload = {}, redirectData: CoreRedirectPayload = {},
urlParams?: CoreUrlParams, urlParams?: CoreUrlParams,
): string { ): Promise<string> {
service = service || CoreConstants.CONFIG.wsservice; service = service || CoreConstants.CONFIG.wsservice;
launchUrl = launchUrl || siteUrl + '/admin/tool/mobile/launch.php'; launchUrl = launchUrl || siteUrl + '/admin/tool/mobile/launch.php';
@ -802,7 +820,7 @@ export class CoreLoginHelperProvider {
// Store the siteurl and passport in CoreConfigProvider for persistence. // Store the siteurl and passport in CoreConfigProvider for persistence.
// We are "configuring" the app to wait for an SSO. CoreConfigProvider shouldn't be used as a temporary storage. // We are "configuring" the app to wait for an SSO. CoreConfigProvider shouldn't be used as a temporary storage.
CoreConfig.set(CoreConstants.LOGIN_LAUNCH_DATA, JSON.stringify(<StoredLoginLaunchData> { await CoreConfig.set(CoreConstants.LOGIN_LAUNCH_DATA, JSON.stringify(<StoredLoginLaunchData> {
siteUrl: siteUrl, siteUrl: siteUrl,
passport: passport, passport: passport,
...redirectData, ...redirectData,