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({ |
Matteo Scandolo | 40f8fa9 | 2016-12-07 09:21:35 -0800 | [diff] [blame] | 9 | selector: '[xosProtected]', |
Matteo Scandolo | 43ffb67 | 2016-12-02 14:49:58 -0800 | [diff] [blame] | 10 | providers: [AuthService] |
| 11 | }) |
| 12 | export class ProtectedDirective implements OnDestroy { |
Matteo Scandolo | 43ffb67 | 2016-12-02 14:49:58 -0800 | [diff] [blame] | 13 | |
Matteo Scandolo | 40f8fa9 | 2016-12-07 09:21:35 -0800 | [diff] [blame] | 14 | constructor(private authService: AuthService, private router: Router) { |
| 15 | if (!this.authService.isAuthenticated()) { |
Matteo Scandolo | 43ffb67 | 2016-12-02 14:49:58 -0800 | [diff] [blame] | 16 | this.router.navigate(['/login']); |
| 17 | } |
Matteo Scandolo | 40f8fa9 | 2016-12-07 09:21:35 -0800 | [diff] [blame] | 18 | // TODO listen for logout |
Matteo Scandolo | 43ffb67 | 2016-12-02 14:49:58 -0800 | [diff] [blame] | 19 | } |
| 20 | |
| 21 | ngOnDestroy() { |
| 22 | // if (this.sub != null) { |
| 23 | // this.sub.unsubscribe(); |
| 24 | // } |
| 25 | } |
| 26 | } |