| 
									
										
										
										
											2016-07-16 17:34:26 -05:00
										 |  |  | // #docregion
 | 
					
						
							| 
									
										
										
										
											2016-08-26 23:19:27 -05:00
										 |  |  | import { Injectable }       from '@angular/core'; | 
					
						
							|  |  |  | import { | 
					
						
							|  |  |  |   CanActivate, Router, | 
					
						
							|  |  |  |   ActivatedRouteSnapshot, | 
					
						
							|  |  |  |   RouterStateSnapshot | 
					
						
							|  |  |  | }                           from '@angular/router'; | 
					
						
							|  |  |  | import { AuthService }      from './auth.service'; | 
					
						
							| 
									
										
										
										
											2016-07-16 17:34:26 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | @Injectable() | 
					
						
							|  |  |  | export class AuthGuard implements CanActivate { | 
					
						
							|  |  |  |   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); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-26 23:19:27 -05:00
										 |  |  |     // Navigate to the login page with extras
 | 
					
						
							| 
									
										
										
										
											2016-07-16 17:34:26 -05:00
										 |  |  |     this.router.navigate(['/login']); | 
					
						
							|  |  |  |     return false; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2016-08-26 23:19:27 -05:00
										 |  |  | // #enddocregion
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* | 
					
						
							|  |  |  | // #docregion can-load-interface
 | 
					
						
							|  |  |  | export class AuthGuard implements CanActivate, CanLoad { | 
					
						
							|  |  |  | // #enddocregion can-load-interface
 | 
					
						
							|  |  |  | */ |