diff --git a/src/app/app-routing.module.ts b/src/app/app-routing.module.ts index 388b3be63..2a8d0f660 100644 --- a/src/app/app-routing.module.ts +++ b/src/app/app-routing.module.ts @@ -15,12 +15,9 @@ import { NgModule } from '@angular/core'; import { PreloadAllModules, RouterModule, Routes } from '@angular/router'; +import { AuthGuard } from '@guards/auth.guard'; + const routes: Routes = [ - { - path: '', - redirectTo: 'login', - pathMatch: 'full', - }, { path: 'login', loadChildren: () => import('./core/login/login.module').then( m => m.CoreLoginModule), @@ -30,8 +27,10 @@ const routes: Routes = [ loadChildren: () => import('./core/settings/settings.module').then( m => m.CoreSettingsModule), }, { - path: 'mainmenu', + path: '', loadChildren: () => import('./core/mainmenu/mainmenu.module').then( m => m.CoreMainMenuModule), + canActivate: [AuthGuard], + canLoad: [AuthGuard], }, ]; diff --git a/src/app/app.component.html b/src/app/app.component.html index 9b06aaff5..94c506c21 100644 --- a/src/app/app.component.html +++ b/src/app/app.component.html @@ -1,3 +1,5 @@ + + diff --git a/src/app/app.component.test.ts b/src/app/app.component.test.ts index 72baacfd2..9036f4b7b 100644 --- a/src/app/app.component.test.ts +++ b/src/app/app.component.test.ts @@ -54,4 +54,6 @@ describe('AppComponent', () => { expect(navController.navigateRoot).toHaveBeenCalledWith('/login/sites'); }); + it.todo('shows loading while app isn\'t ready'); + }); diff --git a/src/app/core/login/services/helper.ts b/src/app/core/login/services/helper.ts index 126f29958..abeee7248 100644 --- a/src/app/core/login/services/helper.ts +++ b/src/app/core/login/services/helper.ts @@ -720,7 +720,7 @@ export class CoreLoginHelperProvider { if (options?.redirectPage == CoreLoginHelperProvider.OPEN_COURSE) { // Load the main menu first, and then open the course. try { - await this.navCtrl.navigateRoot('/mainmenu'); + await this.navCtrl.navigateRoot('/'); } finally { // @todo: Open course. } @@ -729,7 +729,7 @@ export class CoreLoginHelperProvider { const queryParams: Params = Object.assign({}, options); delete queryParams.navigationOptions; - await this.navCtrl.navigateRoot('/mainmenu', { + await this.navCtrl.navigateRoot('/', { queryParams, ...options?.navigationOptions, }); diff --git a/src/app/guards/auth.guard.ts b/src/app/guards/auth.guard.ts new file mode 100644 index 000000000..9319a4b36 --- /dev/null +++ b/src/app/guards/auth.guard.ts @@ -0,0 +1,40 @@ +// (C) Copyright 2015 Moodle Pty Ltd. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +import { Injectable } from '@angular/core'; +import { Router, CanLoad, CanActivate, UrlTree } from '@angular/router'; + +import { CoreInit } from '@services/init'; +import { CoreSites } from '@services/sites'; + +@Injectable({ providedIn: 'root' }) +export class AuthGuard implements CanLoad, CanActivate { + + constructor(private router: Router) {} + + canActivate(): Promise { + return this.guard(); + } + + canLoad(): Promise { + return this.guard(); + } + + private async guard(): Promise { + await CoreInit.instance.ready(); + + return CoreSites.instance.isLoggedIn() || this.router.parseUrl('/login'); + } + +} diff --git a/tsconfig.app.json b/tsconfig.app.json index ae7b6267b..72a9b5489 100644 --- a/tsconfig.app.json +++ b/tsconfig.app.json @@ -17,6 +17,7 @@ "@components/*": ["app/components/*"], "@core/*": ["app/core/*"], "@directives/*": ["app/directives/*"], + "@guards/*": ["app/guards/*"], "@pipes/*": ["app/pipes/*"], "@services/*": ["app/services/*"], "@singletons/*": ["app/singletons/*"] diff --git a/tsconfig.json b/tsconfig.json index b2401e638..1bc3663d1 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -36,6 +36,7 @@ "@components/*": ["app/components/*"], "@core/*": ["app/core/*"], "@directives/*": ["app/directives/*"], + "@guards/*": ["app/guards/*"], "@pipes/*": ["app/pipes/*"], "@services/*": ["app/services/*"], "@singletons/*": ["app/singletons/*"], diff --git a/tsconfig.test.json b/tsconfig.test.json index 06e4ff33d..c62c89cf9 100644 --- a/tsconfig.test.json +++ b/tsconfig.test.json @@ -22,6 +22,7 @@ "@components/*": ["app/components/*"], "@core/*": ["app/core/*"], "@directives/*": ["app/directives/*"], + "@guards/*": ["app/guards/*"], "@pipes/*": ["app/pipes/*"], "@services/*": ["app/services/*"], "@singletons/*": ["app/singletons/*"],