Matteo Scandolo | 43ffb67 | 2016-12-02 14:49:58 -0800 | [diff] [blame] | 1 | /** |
| 2 | * Created by teone on 12/5/16. |
| 3 | */ |
| 4 | import {Directive, OnDestroy} from '@angular/core'; |
| 5 | import {AuthService} from '../services/rest/auth.service'; |
| 6 | import {Router} from '@angular/router'; |
| 7 | |
| 8 | @Directive({ |
| 9 | selector: '[xos-protected]', |
| 10 | providers: [AuthService] |
| 11 | }) |
| 12 | export class ProtectedDirective implements OnDestroy { |
| 13 | private sub:any = null; |
| 14 | |
| 15 | constructor(private authService:AuthService, private router:Router) { |
| 16 | console.log('protected directive'); |
| 17 | if (!authService.isAuthenticated()) { |
| 18 | console.log('redirect'); |
| 19 | this.router.navigate(['/login']); |
| 20 | } |
| 21 | |
| 22 | // this.sub = this.authService.subscribe((val) => { |
| 23 | // if (!val.authenticated) { |
| 24 | // this.router.navigate(['LoggedoutPage']); // tells them they've been logged out (somehow) |
| 25 | // } |
| 26 | // }); |
| 27 | } |
| 28 | |
| 29 | ngOnDestroy() { |
| 30 | // if (this.sub != null) { |
| 31 | // this.sub.unsubscribe(); |
| 32 | // } |
| 33 | } |
| 34 | } |