forked from EVOgeek/Vmeda.Online
		
	MOBILE-3320 routes: Guard authenticated routes
This commit is contained in:
		
							parent
							
								
									7a3dd22189
								
							
						
					
					
						commit
						d8aca97906
					
				| @ -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], | ||||
|     }, | ||||
| ]; | ||||
| 
 | ||||
|  | ||||
| @ -1,3 +1,5 @@ | ||||
| <ion-app> | ||||
|     <!-- @todo move /login/init UI here --> | ||||
| 
 | ||||
|     <ion-router-outlet></ion-router-outlet> | ||||
| </ion-app> | ||||
|  | ||||
| @ -54,4 +54,6 @@ describe('AppComponent', () => { | ||||
|         expect(navController.navigateRoot).toHaveBeenCalledWith('/login/sites'); | ||||
|     }); | ||||
| 
 | ||||
|     it.todo('shows loading while app isn\'t ready'); | ||||
| 
 | ||||
| }); | ||||
|  | ||||
| @ -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, | ||||
|             }); | ||||
|  | ||||
							
								
								
									
										40
									
								
								src/app/guards/auth.guard.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										40
									
								
								src/app/guards/auth.guard.ts
									
									
									
									
									
										Normal file
									
								
							| @ -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<true | UrlTree> { | ||||
|         return this.guard(); | ||||
|     } | ||||
| 
 | ||||
|     canLoad(): Promise<true | UrlTree> { | ||||
|         return this.guard(); | ||||
|     } | ||||
| 
 | ||||
|     private async guard(): Promise<true | UrlTree> { | ||||
|         await CoreInit.instance.ready(); | ||||
| 
 | ||||
|         return CoreSites.instance.isLoggedIn() || this.router.parseUrl('/login'); | ||||
|     } | ||||
| 
 | ||||
| } | ||||
| @ -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/*"] | ||||
|  | ||||
| @ -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/*"], | ||||
|  | ||||
| @ -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/*"], | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user