diff --git a/src/addons/messages/guards/index.ts b/src/addons/messages/guards/index.ts index 4863fa74a..f365adba1 100644 --- a/src/addons/messages/guards/index.ts +++ b/src/addons/messages/guards/index.ts @@ -12,39 +12,23 @@ // See the License for the specific language governing permissions and // limitations under the License. -import { Injectable } from '@angular/core'; -import { ActivatedRouteSnapshot, CanActivate, UrlTree } from '@angular/router'; +import { ActivatedRouteSnapshot, CanActivateFn } from '@angular/router'; import { Router } from '@singletons'; import { AddonMessagesMainMenuHandlerService } from '../services/handlers/mainmenu'; import { AddonMessages } from '../services/messages'; /** * Guard to redirect to the right page based on the current Moodle site version. + * + * @returns Route. */ -@Injectable({ providedIn: 'root' }) -export class AddonMessagesIndexGuard implements CanActivate { +export const messagesIndexGuard: CanActivateFn = async (route: ActivatedRouteSnapshot) => { + const enabled = AddonMessages.isGroupMessagingEnabled(); + const path = `/main/${AddonMessagesMainMenuHandlerService.PAGE_NAME}/` + ( enabled ? 'group-conversations' : 'index'); - /** - * @inheritdoc - */ - canActivate(route: ActivatedRouteSnapshot): UrlTree { - return this.guard(route); - } + const newRoute = Router.parseUrl(path); - /** - * Check if there is a pending redirect and trigger it. - * - * @returns The redirection route. - */ - private guard(route: ActivatedRouteSnapshot): UrlTree { - const enabled = AddonMessages.isGroupMessagingEnabled(); - const path = `/main/${AddonMessagesMainMenuHandlerService.PAGE_NAME}/` + ( enabled ? 'group-conversations' : 'index'); + newRoute.queryParams = route.queryParams; - const newRoute = Router.parseUrl(path); - - newRoute.queryParams = route.queryParams; - - return newRoute; - } - -} + return newRoute; +}; diff --git a/src/addons/messages/messages-lazy.module.ts b/src/addons/messages/messages-lazy.module.ts index 39fef0ea5..e5db4055f 100644 --- a/src/addons/messages/messages-lazy.module.ts +++ b/src/addons/messages/messages-lazy.module.ts @@ -28,7 +28,7 @@ import { CoreMainMenuComponentsModule } from '@features/mainmenu/components/comp import { buildTabMainRoutes } from '@features/mainmenu/mainmenu-tab-routing.module'; import { CoreSearchComponentsModule } from '@features/search/components/components.module'; import { CoreScreen } from '@services/screen'; -import { AddonMessagesIndexGuard } from './guards'; +import { messagesIndexGuard } from './guards'; /** * Build module routes. @@ -120,7 +120,7 @@ function buildRoutes(injector: Injector): Routes { loadChildren: () => import('./messages-settings-lazy.module').then(m => m.AddonMessagesSettingsLazyModule), }, ...buildTabMainRoutes(injector, { - canActivate: [AddonMessagesIndexGuard], + canActivate: [messagesIndexGuard], }), ]; } diff --git a/src/addons/mod/assign/assign-lazy.module.ts b/src/addons/mod/assign/assign-lazy.module.ts index c8dbb6778..c1ce4dfba 100644 --- a/src/addons/mod/assign/assign-lazy.module.ts +++ b/src/addons/mod/assign/assign-lazy.module.ts @@ -16,7 +16,7 @@ import { conditionalRoutes } from '@/app/app-routing.module'; import { CoreSharedModule } from '@/core/shared.module'; import { NgModule } from '@angular/core'; import { RouterModule, Routes } from '@angular/router'; -import { CanLeaveGuard } from '@guards/can-leave'; +import { canLeaveGuard } from '@guards/can-leave'; import { CoreScreen } from '@services/screen'; import { AddonModAssignComponentsModule } from './components/components.module'; import { AddonModAssignEditPage } from './pages/edit/edit'; @@ -32,7 +32,7 @@ const commonRoutes: Routes = [ { path: ':courseId/:cmId/edit', component: AddonModAssignEditPage, - canDeactivate: [CanLeaveGuard], + canDeactivate: [canLeaveGuard], }, ]; @@ -45,7 +45,7 @@ const mobileRoutes: Routes = [ { path: ':courseId/:cmId/submission/:submitId', component: AddonModAssignSubmissionReviewPage, - canDeactivate: [CanLeaveGuard], + canDeactivate: [canLeaveGuard], }, ]; @@ -58,7 +58,7 @@ const tabletRoutes: Routes = [ { path: ':submitId', component: AddonModAssignSubmissionReviewPage, - canDeactivate: [CanLeaveGuard], + canDeactivate: [canLeaveGuard], }, ], }, diff --git a/src/addons/mod/chat/chat-lazy.module.ts b/src/addons/mod/chat/chat-lazy.module.ts index 0a41f9cbc..60cec6c44 100644 --- a/src/addons/mod/chat/chat-lazy.module.ts +++ b/src/addons/mod/chat/chat-lazy.module.ts @@ -22,7 +22,7 @@ import { AddonModChatSessionMessagesPage } from './pages/session-messages/sessio import { CoreScreen } from '@services/screen'; import { conditionalRoutes } from '@/app/app-routing.module'; import { AddonModChatSessionsPage } from './pages/sessions/sessions'; -import { CanLeaveGuard } from '@guards/can-leave'; +import { canLeaveGuard } from '@guards/can-leave'; const commonRoutes: Routes = [ { @@ -32,7 +32,7 @@ const commonRoutes: Routes = [ { path: ':courseId/:cmId/chat', component: AddonModChatChatPage, - canDeactivate: [CanLeaveGuard], + canDeactivate: [canLeaveGuard], }, ]; diff --git a/src/addons/mod/data/data-lazy.module.ts b/src/addons/mod/data/data-lazy.module.ts index 955b03a35..e1f6f4dee 100644 --- a/src/addons/mod/data/data-lazy.module.ts +++ b/src/addons/mod/data/data-lazy.module.ts @@ -18,7 +18,7 @@ import { RouterModule, Routes } from '@angular/router'; import { CoreCommentsComponentsModule } from '@features/comments/components/components.module'; import { CoreCompileHtmlComponentModule } from '@features/compile/components/compile-html/compile-html.module'; import { CoreRatingComponentsModule } from '@features/rating/components/components.module'; -import { CanLeaveGuard } from '@guards/can-leave'; +import { canLeaveGuard } from '@guards/can-leave'; import { AddonModDataComponentsCompileModule } from './components/components-compile.module'; import { AddonModDataComponentsModule } from './components/components.module'; import { AddonModDataEditPage } from './pages/edit/edit'; @@ -33,12 +33,12 @@ const routes: Routes = [ { path: ':courseId/:cmId/edit', component: AddonModDataEditPage, - canDeactivate: [CanLeaveGuard], + canDeactivate: [canLeaveGuard], }, { path: ':courseId/:cmId/edit/:entryId', component: AddonModDataEditPage, - canDeactivate: [CanLeaveGuard], + canDeactivate: [canLeaveGuard], }, { path: ':courseId/:cmId/:entryId', diff --git a/src/addons/mod/feedback/feedback-lazy.module.ts b/src/addons/mod/feedback/feedback-lazy.module.ts index 660c257ce..f34edca90 100644 --- a/src/addons/mod/feedback/feedback-lazy.module.ts +++ b/src/addons/mod/feedback/feedback-lazy.module.ts @@ -22,7 +22,7 @@ import { conditionalRoutes } from '@/app/app-routing.module'; import { CoreScreen } from '@services/screen'; import { AddonModFeedbackAttemptPage } from '@addons/mod/feedback/pages/attempt/attempt'; import { AddonModFeedbackFormPage } from '@addons/mod/feedback/pages/form/form'; -import { CanLeaveGuard } from '@guards/can-leave'; +import { canLeaveGuard } from '@guards/can-leave'; import { AddonModFeedbackNonRespondentsPage } from '@addons/mod/feedback/pages/nonrespondents/nonrespondents'; const commonRoutes: Routes = [ @@ -33,7 +33,7 @@ const commonRoutes: Routes = [ { path: ':courseId/:cmId/form', component: AddonModFeedbackFormPage, - canDeactivate: [CanLeaveGuard], + canDeactivate: [canLeaveGuard], }, { path: ':courseId/:cmId/nonrespondents', diff --git a/src/addons/mod/forum/forum-discussion-lazy.module.ts b/src/addons/mod/forum/forum-discussion-lazy.module.ts index 697816702..a365aacc9 100644 --- a/src/addons/mod/forum/forum-discussion-lazy.module.ts +++ b/src/addons/mod/forum/forum-discussion-lazy.module.ts @@ -16,14 +16,14 @@ import { NgModule } from '@angular/core'; import { RouterModule, Routes } from '@angular/router'; import { AddonModForumComponentsModule } from '@addons/mod/forum/components/components.module'; -import { CanLeaveGuard } from '@guards/can-leave'; +import { canLeaveGuard } from '@guards/can-leave'; import { CoreSharedModule } from '@/core/shared.module'; import { AddonModForumDiscussionPage } from '@addons/mod/forum/pages/discussion/discussion'; const routes: Routes = [{ path: '', component: AddonModForumDiscussionPage, - canDeactivate: [CanLeaveGuard], + canDeactivate: [canLeaveGuard], }]; @NgModule({ diff --git a/src/addons/mod/forum/forum-new-discussion-lazy.module.ts b/src/addons/mod/forum/forum-new-discussion-lazy.module.ts index e0007654b..a1224b72b 100644 --- a/src/addons/mod/forum/forum-new-discussion-lazy.module.ts +++ b/src/addons/mod/forum/forum-new-discussion-lazy.module.ts @@ -16,7 +16,7 @@ import { NgModule } from '@angular/core'; import { RouterModule, Routes } from '@angular/router'; import { AddonModForumComponentsModule } from '@addons/mod/forum/components/components.module'; -import { CanLeaveGuard } from '@guards/can-leave'; +import { canLeaveGuard } from '@guards/can-leave'; import { CoreEditorComponentsModule } from '@features/editor/components/components.module'; import { CoreSharedModule } from '@/core/shared.module'; import { AddonModForumNewDiscussionPage } from '@addons/mod/forum/pages/new-discussion/new-discussion'; @@ -24,7 +24,7 @@ import { AddonModForumNewDiscussionPage } from '@addons/mod/forum/pages/new-disc const routes: Routes = [{ path: '', component: AddonModForumNewDiscussionPage, - canDeactivate: [CanLeaveGuard], + canDeactivate: [canLeaveGuard], }]; @NgModule({ diff --git a/src/addons/mod/glossary/glossary-edit-lazy.module.ts b/src/addons/mod/glossary/glossary-edit-lazy.module.ts index 990aa6b2c..0107cdaff 100644 --- a/src/addons/mod/glossary/glossary-edit-lazy.module.ts +++ b/src/addons/mod/glossary/glossary-edit-lazy.module.ts @@ -16,13 +16,13 @@ import { NgModule } from '@angular/core'; import { CoreSharedModule } from '@/core/shared.module'; import { CoreEditorComponentsModule } from '@features/editor/components/components.module'; import { RouterModule, Routes } from '@angular/router'; -import { CanLeaveGuard } from '@guards/can-leave'; +import { canLeaveGuard } from '@guards/can-leave'; import { AddonModGlossaryEditPage } from '@addons/mod/glossary/pages/edit/edit'; const routes: Routes = [{ path: '', component: AddonModGlossaryEditPage, - canDeactivate: [CanLeaveGuard], + canDeactivate: [canLeaveGuard], }]; @NgModule({ diff --git a/src/addons/mod/h5pactivity/h5pactivity-lazy.module.ts b/src/addons/mod/h5pactivity/h5pactivity-lazy.module.ts index 39999538a..fe4312444 100644 --- a/src/addons/mod/h5pactivity/h5pactivity-lazy.module.ts +++ b/src/addons/mod/h5pactivity/h5pactivity-lazy.module.ts @@ -16,7 +16,7 @@ import { NgModule } from '@angular/core'; import { RouterModule, Routes } from '@angular/router'; import { CoreSharedModule } from '@/core/shared.module'; -import { CanLeaveGuard } from '@guards/can-leave'; +import { canLeaveGuard } from '@guards/can-leave'; import { AddonModH5PActivityComponentsModule } from './components/components.module'; import { AddonModH5PActivityIndexPage } from './pages/index/index'; import { AddonModH5PActivityUserAttemptsPage } from '@addons/mod/h5pactivity/pages/user-attempts/user-attempts'; @@ -27,7 +27,7 @@ const routes: Routes = [ { path: ':courseId/:cmId', component: AddonModH5PActivityIndexPage, - canDeactivate: [CanLeaveGuard], + canDeactivate: [canLeaveGuard], }, { path: ':courseId/:cmId/userattempts/:userId', diff --git a/src/addons/mod/lesson/lesson-lazy.module.ts b/src/addons/mod/lesson/lesson-lazy.module.ts index 880b69126..749b6547f 100644 --- a/src/addons/mod/lesson/lesson-lazy.module.ts +++ b/src/addons/mod/lesson/lesson-lazy.module.ts @@ -21,7 +21,7 @@ import { AddonModLessonComponentsModule } from './components/components.module'; import { AddonModLessonIndexPage } from './pages/index'; import { CoreEditorComponentsModule } from '@features/editor/components/components.module'; import { AddonModLessonPlayerPage } from '@addons/mod/lesson/pages/player/player'; -import { CanLeaveGuard } from '@guards/can-leave'; +import { canLeaveGuard } from '@guards/can-leave'; import { AddonModLessonUserRetakePage } from '@addons/mod/lesson/pages/user-retake/user-retake'; const routes: Routes = [ @@ -32,7 +32,7 @@ const routes: Routes = [ { path: ':courseId/:cmId/player', component: AddonModLessonPlayerPage, - canDeactivate: [CanLeaveGuard], + canDeactivate: [canLeaveGuard], }, { path: ':courseId/:cmId/user-retake/:userId', diff --git a/src/addons/mod/quiz/quiz-lazy.module.ts b/src/addons/mod/quiz/quiz-lazy.module.ts index 36c01811e..51f1e3d04 100644 --- a/src/addons/mod/quiz/quiz-lazy.module.ts +++ b/src/addons/mod/quiz/quiz-lazy.module.ts @@ -22,7 +22,7 @@ import { AddonModQuizIndexPage } from './pages/index'; import { AddonModQuizAttemptPage } from '@addons/mod/quiz/pages/attempt/attempt'; import { CoreQuestionComponentsModule } from '@features/question/components/components.module'; import { AddonModQuizPlayerPage } from '@addons/mod/quiz/pages/player/player'; -import { CanLeaveGuard } from '@guards/can-leave'; +import { canLeaveGuard } from '@guards/can-leave'; import { AddonModQuizReviewPage } from '@addons/mod/quiz/pages/review/review'; const routes: Routes = [ @@ -33,7 +33,7 @@ const routes: Routes = [ { path: ':courseId/:cmId/player', component: AddonModQuizPlayerPage, - canDeactivate: [CanLeaveGuard], + canDeactivate: [canLeaveGuard], }, { path: ':courseId/:cmId/attempt/:attemptId', diff --git a/src/addons/mod/wiki/wiki-lazy.module.ts b/src/addons/mod/wiki/wiki-lazy.module.ts index 3c7225328..354ad83aa 100644 --- a/src/addons/mod/wiki/wiki-lazy.module.ts +++ b/src/addons/mod/wiki/wiki-lazy.module.ts @@ -18,7 +18,7 @@ import { CoreSharedModule } from '@/core/shared.module'; import { AddonModWikiComponentsModule } from './components/components.module'; import { AddonModWikiIndexPage } from './pages/index/index'; import { CoreEditorComponentsModule } from '@features/editor/components/components.module'; -import { CanLeaveGuard } from '@guards/can-leave'; +import { canLeaveGuard } from '@guards/can-leave'; import { AddonModWikiEditPage } from './pages/edit/edit'; const routes: Routes = [ @@ -33,7 +33,7 @@ const routes: Routes = [ { path: ':courseId/:cmId/edit', component: AddonModWikiEditPage, - canDeactivate: [CanLeaveGuard], + canDeactivate: [canLeaveGuard], }, ]; diff --git a/src/addons/mod/workshop/workshop-lazy.module.ts b/src/addons/mod/workshop/workshop-lazy.module.ts index 7199432d5..a49cddae5 100644 --- a/src/addons/mod/workshop/workshop-lazy.module.ts +++ b/src/addons/mod/workshop/workshop-lazy.module.ts @@ -14,7 +14,7 @@ import { NgModule } from '@angular/core'; import { RouterModule, Routes } from '@angular/router'; -import { CanLeaveGuard } from '@guards/can-leave'; +import { canLeaveGuard } from '@guards/can-leave'; import { CoreSharedModule } from '@/core/shared.module'; import { AddonModWorkshopIndexPage } from './pages/index/index'; @@ -32,17 +32,17 @@ const routes: Routes = [ { path: ':courseId/:cmId/:submissionId', component: AddonModWorkshopSubmissionPage, - canDeactivate: [CanLeaveGuard], + canDeactivate: [canLeaveGuard], }, { path: ':courseId/:cmId/:submissionId/edit', component: AddonModWorkshopEditSubmissionPage, - canDeactivate: [CanLeaveGuard], + canDeactivate: [canLeaveGuard], }, { path: ':courseId/:cmId/:submissionId/:assessmentId', component: AddonModWorkshopAssessmentPage, - canDeactivate: [CanLeaveGuard], + canDeactivate: [canLeaveGuard], }, ]; diff --git a/src/core/features/login/guards/has-sites.ts b/src/core/features/login/guards/has-sites.ts index a96745941..af2b7f9d8 100644 --- a/src/core/features/login/guards/has-sites.ts +++ b/src/core/features/login/guards/has-sites.ts @@ -12,50 +12,28 @@ // See the License for the specific language governing permissions and // limitations under the License. -import { Injectable } from '@angular/core'; -import { CanActivate, CanLoad, UrlTree } from '@angular/router'; - +import { CanActivateFn } from '@angular/router'; import { CoreSites } from '@services/sites'; import { CoreUtils } from '@services/utils/utils'; import { Router } from '@singletons'; - import { CoreLoginHelper } from '../services/login-helper'; -@Injectable({ providedIn: 'root' }) -export class CoreLoginHasSitesGuard implements CanActivate, CanLoad { +/** + * Guard to check if the user has any sites stored. + * + * @returns True if user has sites, redirect route otherwise. + */ +export const hasSitesGuard: CanActivateFn = async () => { + const sites = await CoreUtils.ignoreErrors(CoreSites.getSites(), []); - /** - * @inheritdoc - */ - canActivate(): Promise { - return this.guard(); + if (sites.length > 0) { + return true; } - /** - * @inheritdoc - */ - canLoad(): Promise { - return this.guard(); - } + const [path, params] = await CoreLoginHelper.getAddSiteRouteInfo(); + const route = Router.parseUrl(path); - /** - * Check if the user has any sites stored. - * - * @returns Promise resolved with true if it's not redirected or the redirection route. - */ - private async guard(): Promise { - const sites = await CoreUtils.ignoreErrors(CoreSites.getSites(), []); + route.queryParams = params; - if (sites.length > 0) { - return true; - } - - const [path, params] = await CoreLoginHelper.getAddSiteRouteInfo(); - const route = Router.parseUrl(path); - - route.queryParams = params; - - return route; - } - -} + return route; +}; diff --git a/src/core/features/login/login-lazy.module.ts b/src/core/features/login/login-lazy.module.ts index 4e209d264..0a575f5e7 100644 --- a/src/core/features/login/login-lazy.module.ts +++ b/src/core/features/login/login-lazy.module.ts @@ -16,7 +16,7 @@ import { NgModule } from '@angular/core'; import { RouterModule, Routes } from '@angular/router'; import { CoreSharedModule } from '@/core/shared.module'; -import { CoreLoginHasSitesGuard } from './guards/has-sites'; +import { hasSitesGuard } from './guards/has-sites'; import { CoreLoginComponentsModule } from './components/components.module'; import { CoreLoginHelper } from './services/login-helper'; import { CoreLoginForgottenPasswordPage } from '@features/login/pages/forgotten-password/forgotten-password'; @@ -44,8 +44,7 @@ const routes: Routes = [ { path: 'sites', component: CoreLoginSitesPage, - canLoad: [CoreLoginHasSitesGuard], - canActivate: [CoreLoginHasSitesGuard], + canActivate: [hasSitesGuard], }, { path: 'forgottenpassword', diff --git a/src/core/features/login/login.module.ts b/src/core/features/login/login.module.ts index ac69177bc..a395dbe74 100644 --- a/src/core/features/login/login.module.ts +++ b/src/core/features/login/login.module.ts @@ -17,7 +17,7 @@ import { Routes } from '@angular/router'; import { AppRoutingModule } from '@/app/app-routing.module'; import { CoreLoginHelper, CoreLoginHelperProvider } from './services/login-helper'; -import { CoreRedirectGuard } from '@guards/redirect'; +import { redirectGuard } from '@guards/redirect'; import { CoreLoginCronHandler } from './services/handlers/cron'; import { CoreCronDelegate } from '@services/cron'; import { CoreEvents } from '@singletons/events'; @@ -30,8 +30,7 @@ const appRoutes: Routes = [ { path: 'login', loadChildren: () => import('./login-lazy.module').then(m => m.CoreLoginLazyModule), - canActivate: [CoreRedirectGuard], - canLoad: [CoreRedirectGuard], + canActivate: [redirectGuard], }, ]; diff --git a/src/core/features/mainmenu/guards/auth.ts b/src/core/features/mainmenu/guards/auth.ts index cd16ea2aa..68923cb9b 100644 --- a/src/core/features/mainmenu/guards/auth.ts +++ b/src/core/features/mainmenu/guards/auth.ts @@ -12,61 +12,41 @@ // See the License for the specific language governing permissions and // limitations under the License. -import { Injectable } from '@angular/core'; -import { CanLoad, CanActivate, UrlTree } from '@angular/router'; +import { CanActivateFn } from '@angular/router'; import { CoreLoginHelper } from '@features/login/services/login-helper'; import { CoreApp } from '@services/app'; import { CoreSites } from '@services/sites'; import { Router } from '@singletons'; -@Injectable({ providedIn: 'root' }) -export class CoreMainMenuAuthGuard implements CanLoad, CanActivate { - - /** - * @inheritdoc - */ - canActivate(): Promise { - return this.guard(); +/** + * Guard to check if the user is authenticated. + * + * @returns True if user has sites, redirect route otherwise. + */ +export const authGuard: CanActivateFn = async () => { + if (!CoreSites.isLoggedIn()) { + return Router.parseUrl('/login'); } - /** - * @inheritdoc - */ - canLoad(): Promise { - return this.guard(); - } + if (CoreLoginHelper.isSiteLoggedOut()) { + // Send the user to reconnect page. + const newRoute = Router.parseUrl('/login/reconnect'); + const siteId = CoreSites.getCurrentSiteId(); - /** - * Check if the current user should be redirected to the authentication page. - * - * @returns Promise resolved with true if it's not redirected or the redirection route. - */ - private async guard(): Promise { - if (!CoreSites.isLoggedIn()) { - return Router.parseUrl('/login'); + // Pass redirect data (if any and belongs to same site). + let redirect = CoreApp.consumeMemoryRedirect(); + if (redirect?.siteId !== siteId) { + redirect = null; } - if (CoreLoginHelper.isSiteLoggedOut()) { - // Send the user to reconnect page. - const newRoute = Router.parseUrl('/login/reconnect'); - const siteId = CoreSites.getCurrentSiteId(); + newRoute.queryParams = { + siteId, + ...redirect, + }; - // Pass redirect data (if any and belongs to same site). - let redirect = CoreApp.consumeMemoryRedirect(); - if (redirect?.siteId !== siteId) { - redirect = null; - } - - newRoute.queryParams = { - siteId, - ...redirect, - }; - - return newRoute; - } - - return true; + return newRoute; } -} + return true; +}; diff --git a/src/core/features/mainmenu/mainmenu.module.ts b/src/core/features/mainmenu/mainmenu.module.ts index 361a01b69..1f1d308c0 100644 --- a/src/core/features/mainmenu/mainmenu.module.ts +++ b/src/core/features/mainmenu/mainmenu.module.ts @@ -14,7 +14,7 @@ import { APP_INITIALIZER, NgModule } from '@angular/core'; import { Routes } from '@angular/router'; -import { CoreMainMenuAuthGuard } from '@features/mainmenu/guards/auth'; +import { authGuard } from '@features/mainmenu/guards/auth'; import { AppRoutingModule } from '@/app/app-routing.module'; @@ -38,8 +38,7 @@ const appRoutes: Routes = [ { path: 'main', loadChildren: () => import('./mainmenu-lazy.module').then(m => m.CoreMainMenuLazyModule), - canActivate: [CoreMainMenuAuthGuard], - canLoad: [CoreMainMenuAuthGuard], + canActivate: [authGuard], }, { path: 'reload', diff --git a/src/core/features/siteplugins/siteplugins.module.ts b/src/core/features/siteplugins/siteplugins.module.ts index 796e15e1e..280baee8b 100644 --- a/src/core/features/siteplugins/siteplugins.module.ts +++ b/src/core/features/siteplugins/siteplugins.module.ts @@ -23,7 +23,7 @@ import { CoreSitePluginsComponentsModule } from './components/components.module' import { CoreSitePluginsHelper } from './services/siteplugins-helper'; import { CoreSharedModule } from '@/core/shared.module'; import { CoreSitePluginsPluginPage } from '@features/siteplugins/pages/plugin/plugin'; -import { CanLeaveGuard } from '@guards/can-leave'; +import { canLeaveGuard } from '@guards/can-leave'; import { CoreSitePluginsCourseOptionPage } from '@features/siteplugins/pages/course-option/course-option'; import { CoreSitePluginsModuleIndexPage } from '@features/siteplugins/pages/module-index/module-index'; @@ -31,7 +31,7 @@ const routes: Routes = [ { path: 'siteplugins/content/:component/:method/:hash', component: CoreSitePluginsPluginPage, - canDeactivate: [CanLeaveGuard], + canDeactivate: [canLeaveGuard], }, ]; @@ -39,7 +39,7 @@ const homeRoutes: Routes = [ { path: 'siteplugins/homecontent/:component/:method', component: CoreSitePluginsPluginPage, - canDeactivate: [CanLeaveGuard], + canDeactivate: [canLeaveGuard], }, ]; @@ -47,7 +47,7 @@ const courseIndexRoutes: Routes = [ { path: 'siteplugins/:handlerUniqueName', component: CoreSitePluginsCourseOptionPage, - canDeactivate: [CanLeaveGuard], + canDeactivate: [canLeaveGuard], }, ]; @@ -55,7 +55,7 @@ const moduleRoutes: Routes = [ { path: 'siteplugins/module/:courseId/:cmId', component: CoreSitePluginsModuleIndexPage, - canDeactivate: [CanLeaveGuard], + canDeactivate: [canLeaveGuard], }, ]; diff --git a/src/core/guards/can-leave.ts b/src/core/guards/can-leave.ts index 4c1afbafe..527e70270 100644 --- a/src/core/guards/can-leave.ts +++ b/src/core/guards/can-leave.ts @@ -12,26 +12,30 @@ // See the License for the specific language governing permissions and // limitations under the License. -import { Injectable } from '@angular/core'; -import { CanDeactivate } from '@angular/router'; +import { CanDeactivateFn } from '@angular/router'; import { CoreUtils } from '@services/utils/utils'; -@Injectable({ providedIn: 'root' }) -export class CanLeaveGuard implements CanDeactivate { +/** + * Check if a component implements the canLeave interface. + * + * @param component Component instance to check. + * @returns Whether it implements CanLeave interface. + */ +const isCanLeave = (component: unknown | null): component is CanLeave => + component !== null && 'canLeave' in component; - async canDeactivate(component: unknown | null): Promise { - if (!this.isCanLeave(component)) { - return true; - } - - return CoreUtils.ignoreErrors(component.canLeave(), false); +/** + * Guard to check if the user can leave a page. + * + * @returns True if user has sites, redirect route otherwise. + */ +export const canLeaveGuard: CanDeactivateFn = async (component: unknown) => { + if (!isCanLeave(component)) { + return true; } - isCanLeave(component: unknown | null): component is CanLeave { - return component !== null && 'canLeave' in component; - } - -} + return CoreUtils.ignoreErrors(component.canLeave(), false); +}; export interface CanLeave { /** diff --git a/src/core/guards/redirect.ts b/src/core/guards/redirect.ts index 23e6d5e93..55bf4996f 100644 --- a/src/core/guards/redirect.ts +++ b/src/core/guards/redirect.ts @@ -12,38 +12,20 @@ // See the License for the specific language governing permissions and // limitations under the License. -import { Injectable } from '@angular/core'; -import { CanActivate, CanLoad, UrlTree } from '@angular/router'; +import { CanActivateFn } from '@angular/router'; import { CoreApp } from '@services/app'; import { CoreRedirectPayload } from '@services/navigator'; import { CoreSites } from '@services/sites'; import { Router } from '@singletons'; import { CoreConstants } from '../constants'; -@Injectable({ providedIn: 'root' }) -export class CoreRedirectGuard implements CanLoad, CanActivate { - - /** - * @inheritdoc - */ - canLoad(): Promise { - return this.guard(); - } - - /** - * @inheritdoc - */ - canActivate(): Promise { - return this.guard(); - } - - /** - * Check if there is a pending redirect and trigger it. - * - * @returns Promise resolved with true if it's not redirected or the redirection route. - */ - private async guard(): Promise { - const redirect = CoreApp.consumeMemoryRedirect(); +/** + * Guard to check if there is a pending redirect and trigger it. + * + * @returns True if there's no redirect, redirection route otherwise. + */ +export const redirectGuard: CanActivateFn = async () => { + const redirect = CoreApp.consumeMemoryRedirect(); if (!redirect) { return true; } @@ -81,6 +63,4 @@ export class CoreRedirectGuard implements CanLoad, CanActivate { route.queryParams = redirect.redirectOptions?.params || {}; return route; - } - -} +};