forked from CIT/Vmeda.Online
		
	MOBILE-4028 links: Fix opening links to sites with token expired
This commit is contained in:
		
							parent
							
								
									feff5a87f8
								
							
						
					
					
						commit
						e671ce437a
					
				@ -19,6 +19,7 @@ import { CoreUrl } from '@singletons/url';
 | 
			
		||||
import { makeSingleton } from '@singletons';
 | 
			
		||||
import { CoreText } from '@singletons/text';
 | 
			
		||||
import { CorePromiseUtils } from '@singletons/promise-utils';
 | 
			
		||||
import { CoreNavigator } from '@services/navigator';
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Interface that all handlers must implement.
 | 
			
		||||
@ -208,15 +209,24 @@ export class CoreContentLinksDelegateService {
 | 
			
		||||
                        // Wrap the action function in our own function to treat logged out sites.
 | 
			
		||||
                        const actionFunction = action.action;
 | 
			
		||||
                        action.action = async (siteId) => {
 | 
			
		||||
                            const site = await CoreSites.getSite(siteId);
 | 
			
		||||
                            if (!CoreSites.isLoggedIn()) {
 | 
			
		||||
                                // Not logged in, load site first.
 | 
			
		||||
                                const loggedIn = await CoreSites.loadSite(siteId, { urlToOpen: url });
 | 
			
		||||
                                if (loggedIn) {
 | 
			
		||||
                                    await CoreNavigator.navigateToSiteHome({ params: { urlToOpen: url } });
 | 
			
		||||
                                }
 | 
			
		||||
 | 
			
		||||
                            if (!site.isLoggedOut()) {
 | 
			
		||||
                                // Call the action now.
 | 
			
		||||
                                return actionFunction(siteId);
 | 
			
		||||
                                return;
 | 
			
		||||
                            }
 | 
			
		||||
 | 
			
		||||
                            // Site is logged out, authenticate first before treating the URL.
 | 
			
		||||
                            await CoreSites.logout({ urlToOpen: url, siteId });
 | 
			
		||||
                            if (siteId !== CoreSites.getCurrentSiteId()) {
 | 
			
		||||
                                // Different site, logout and login first before treating the URL because token could be expired.
 | 
			
		||||
                                await CoreSites.logout({ urlToOpen: url, siteId });
 | 
			
		||||
 | 
			
		||||
                                return;
 | 
			
		||||
                            }
 | 
			
		||||
 | 
			
		||||
                            actionFunction(siteId);
 | 
			
		||||
                        };
 | 
			
		||||
                    });
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -33,6 +33,7 @@ describe('Site Home link handlers', () => {
 | 
			
		||||
            getSite: () => Promise.resolve(new CoreSite(siteId, siteUrl, '')),
 | 
			
		||||
            getSiteIdsFromUrl: () => Promise.resolve([siteId]),
 | 
			
		||||
            getCurrentSiteId: () => siteId,
 | 
			
		||||
            isLoggedIn: () => true,
 | 
			
		||||
        }));
 | 
			
		||||
 | 
			
		||||
        mockSingleton(CoreLoginHelper, { getAvailableSites: async () => [{ url: siteUrl, name: 'Example Campus' }] });
 | 
			
		||||
 | 
			
		||||
@ -195,7 +195,7 @@ export class CoreNavigatorService {
 | 
			
		||||
    async navigateToSiteHome(options: Omit<CoreNavigationOptions, 'reset'> & { siteId?: string } = {}): Promise<boolean> {
 | 
			
		||||
        const siteId = options.siteId ?? CoreSites.getCurrentSiteId();
 | 
			
		||||
        const landingPagePath = CoreSites.isLoggedIn() && CoreSites.getCurrentSiteId() === siteId ?
 | 
			
		||||
            this.getLandingTabPage() : 'main';
 | 
			
		||||
            this.getLandingTabPage() : '';
 | 
			
		||||
 | 
			
		||||
        return this.navigateToSitePath(landingPagePath, {
 | 
			
		||||
            ...options,
 | 
			
		||||
@ -221,8 +221,7 @@ export class CoreNavigatorService {
 | 
			
		||||
        // If we are logged into a different site, log out first.
 | 
			
		||||
        if (CoreSites.isLoggedIn() && CoreSites.getCurrentSiteId() !== siteId) {
 | 
			
		||||
            await CoreSites.logout({
 | 
			
		||||
                redirectPath: path,
 | 
			
		||||
                redirectOptions: options || {},
 | 
			
		||||
                ...this.getRedirectDataForSitePath(path, options),
 | 
			
		||||
                siteId,
 | 
			
		||||
            });
 | 
			
		||||
 | 
			
		||||
@ -242,10 +241,7 @@ export class CoreNavigatorService {
 | 
			
		||||
            const modal = await CoreLoadings.show();
 | 
			
		||||
 | 
			
		||||
            try {
 | 
			
		||||
                const loggedIn = await CoreSites.loadSite(siteId, {
 | 
			
		||||
                    redirectPath: path,
 | 
			
		||||
                    redirectOptions: options,
 | 
			
		||||
                });
 | 
			
		||||
                const loggedIn = await CoreSites.loadSite(siteId, this.getRedirectDataForSitePath(path, options));
 | 
			
		||||
 | 
			
		||||
                if (!loggedIn) {
 | 
			
		||||
                    // User has been redirected to the login page and will be redirected to the site path after login.
 | 
			
		||||
@ -263,6 +259,31 @@ export class CoreNavigatorService {
 | 
			
		||||
        return this.navigateToMainMenuPath(path, navigationOptions);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Get the redirect data to use when navigating to a site path.
 | 
			
		||||
     *
 | 
			
		||||
     * @param path Site path.
 | 
			
		||||
     * @param options Navigation options.
 | 
			
		||||
     * @returns Redirect data.
 | 
			
		||||
     */
 | 
			
		||||
    protected getRedirectDataForSitePath(path: string, options: CoreNavigationOptions = {}): CoreRedirectPayload {
 | 
			
		||||
        if (!path || path.match(/^\/?main\/?$/)) {
 | 
			
		||||
            // Navigating to main, obtain the redirect from the navigation parameters (if any).
 | 
			
		||||
            // If there is no redirect path or url to open, use 'main' to open the site's main menu.
 | 
			
		||||
            return {
 | 
			
		||||
                redirectPath: !options.params?.redirectPath && !options.params?.urlToOpen ? 'main' : options.params?.redirectPath,
 | 
			
		||||
                redirectOptions: options.params?.redirectOptions,
 | 
			
		||||
                urlToOpen: options.params?.urlToOpen,
 | 
			
		||||
            };
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        // Use the path to navigate as the redirect path.
 | 
			
		||||
        return {
 | 
			
		||||
            redirectPath: path,
 | 
			
		||||
            redirectOptions: options || {},
 | 
			
		||||
        };
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Get the active route path.
 | 
			
		||||
     *
 | 
			
		||||
@ -546,6 +567,11 @@ export class CoreNavigatorService {
 | 
			
		||||
            ...options,
 | 
			
		||||
        };
 | 
			
		||||
 | 
			
		||||
        if (!path || path.match(/^\/?main\/?$/)) {
 | 
			
		||||
            // Navigating to main, nothing else to do.
 | 
			
		||||
            return this.navigate('/main', options);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        path = path.replace(/^(\.|\/main)?\//, '');
 | 
			
		||||
 | 
			
		||||
        const pathRoot = /^[^/]+/.exec(path)?.[0] ?? '';
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user