blob: 8eac4fd6b361b7679eadc4614cec0bbc096ff995 [file] [log] [blame]
Matteo Scandolo43ffb672016-12-02 14:49:58 -08001/**
2 * Created by teone on 12/5/16.
3 */
4import {Directive, OnDestroy} from '@angular/core';
5import {AuthService} from '../services/rest/auth.service';
6import {Router} from '@angular/router';
7
8@Directive({
Matteo Scandolo40f8fa92016-12-07 09:21:35 -08009 selector: '[xosProtected]',
Matteo Scandolo43ffb672016-12-02 14:49:58 -080010 providers: [AuthService]
11})
12export class ProtectedDirective implements OnDestroy {
Matteo Scandolo43ffb672016-12-02 14:49:58 -080013
Matteo Scandolo40f8fa92016-12-07 09:21:35 -080014 constructor(private authService: AuthService, private router: Router) {
15 if (!this.authService.isAuthenticated()) {
Matteo Scandolo43ffb672016-12-02 14:49:58 -080016 this.router.navigate(['/login']);
17 }
Matteo Scandolo40f8fa92016-12-07 09:21:35 -080018 // TODO listen for logout
Matteo Scandolo43ffb672016-12-02 14:49:58 -080019 }
20
21 ngOnDestroy() {
22 // if (this.sub != null) {
23 // this.sub.unsubscribe();
24 // }
25 }
26}