| 
									
										
										
										
											2016-08-26 23:19:27 -05:00
										 |  |  | // #docplaster
 | 
					
						
							|  |  |  | // #docregion, admin-can-load
 | 
					
						
							|  |  |  | import { Injectable }       from '@angular/core'; | 
					
						
							|  |  |  | import { | 
					
						
							|  |  |  |   CanActivate, Router, | 
					
						
							|  |  |  |   ActivatedRouteSnapshot, | 
					
						
							|  |  |  |   RouterStateSnapshot, | 
					
						
							|  |  |  |   CanActivateChild, | 
					
						
							|  |  |  |   NavigationExtras, | 
					
						
							|  |  |  |   CanLoad, Route | 
					
						
							|  |  |  | }                           from '@angular/router'; | 
					
						
							|  |  |  | import { AuthService }      from './auth.service'; | 
					
						
							| 
									
										
										
										
											2016-07-16 17:34:26 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | @Injectable() | 
					
						
							| 
									
										
										
										
											2016-08-26 23:19:27 -05:00
										 |  |  | export class AuthGuard implements CanActivate, CanActivateChild, CanLoad { | 
					
						
							| 
									
										
										
										
											2016-07-16 17:34:26 -05:00
										 |  |  |   constructor(private authService: AuthService, private router: Router) {} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-26 23:19:27 -05:00
										 |  |  |   canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean { | 
					
						
							|  |  |  |     let url: string = state.url; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return this.checkLogin(url); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   canActivateChild(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean { | 
					
						
							|  |  |  |     return this.canActivate(route, state); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   canLoad(route: Route): boolean { | 
					
						
							|  |  |  |     let url = `/${route.path}`; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return this.checkLogin(url); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | // #enddocregion admin-can-load
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   checkLogin(url: string): boolean { | 
					
						
							| 
									
										
										
										
											2016-07-16 17:34:26 -05:00
										 |  |  |     if (this.authService.isLoggedIn) { return true; } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // Store the attempted URL for redirecting
 | 
					
						
							| 
									
										
										
										
											2016-08-26 23:19:27 -05:00
										 |  |  |     this.authService.redirectUrl = url; | 
					
						
							| 
									
										
										
										
											2016-07-16 17:34:26 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // Create a dummy session id
 | 
					
						
							|  |  |  |     let sessionId = 123456789; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // Set our navigation extras object
 | 
					
						
							|  |  |  |     // that contains our global query params and fragment
 | 
					
						
							| 
									
										
										
										
											2016-08-09 17:38:25 +01:00
										 |  |  |     let navigationExtras: NavigationExtras = { | 
					
						
							| 
									
										
										
										
											2016-07-16 17:34:26 -05:00
										 |  |  |       queryParams: { 'session_id': sessionId }, | 
					
						
							|  |  |  |       fragment: 'anchor' | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // Navigate to the login page with extras
 | 
					
						
							|  |  |  |     this.router.navigate(['/login'], navigationExtras); | 
					
						
							|  |  |  |     return false; | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2016-08-26 23:19:27 -05:00
										 |  |  | // #docregion admin-can-load
 | 
					
						
							| 
									
										
										
										
											2016-07-16 17:34:26 -05:00
										 |  |  | } |