| 
									
										
										
										
											2016-07-16 17:34:26 -05:00
										 |  |  | // #docregion
 | 
					
						
							|  |  |  | import { Injectable }             from '@angular/core'; | 
					
						
							|  |  |  | import { CanActivate, Router, | 
					
						
							|  |  |  |          ActivatedRouteSnapshot, | 
					
						
							| 
									
										
										
										
											2016-08-09 17:38:25 +01:00
										 |  |  |          RouterStateSnapshot, | 
					
						
							|  |  |  |          NavigationExtras }       from '@angular/router'; | 
					
						
							| 
									
										
										
										
											2016-07-16 17:34:26 -05:00
										 |  |  | import { AuthService }            from './auth.service'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @Injectable() | 
					
						
							|  |  |  | export class AuthGuard implements CanActivate { | 
					
						
							|  |  |  |   constructor(private authService: AuthService, private router: Router) {} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) { | 
					
						
							|  |  |  |     if (this.authService.isLoggedIn) { return true; } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // Store the attempted URL for redirecting
 | 
					
						
							|  |  |  |     this.authService.redirectUrl = state.url; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // 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; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | } |