Projekt

Obecné

Profil

Stáhnout (1.1 KB) Statistiky
| Větev: | Tag: | Revize:
1 1c220966 Hung Hoang
import {Injectable} from '@angular/core';
2
import {ActivatedRouteSnapshot, CanActivate, Router, RouterStateSnapshot} from '@angular/router';
3
import {Observable} from 'rxjs';
4
import {ProfileService} from '../services/util/profile.service';
5
import {BaseGuard} from './base-guard';
6
7
@Injectable({
8
  providedIn: 'root'
9
})
10
export class DashboardComponentGuard extends BaseGuard implements CanActivate {
11
12
  constructor(private profileService: ProfileService, protected router: Router) {
13
    super(router);
14
  }
15
16
  /**
17
   * All logged users can navigate to dashboard component
18
   * other users are navigate to login page
19
   * @param route activated route snapshot
20
   * @param state router state snapshot
21
   */
22
  canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> | boolean {
23
    return new Observable<boolean>((obs) => {
24
      this.profileService.getLoggedUser()
25
        .subscribe(() => {
26
            obs.next(true);
27
            obs.complete();
28
          },
29
          () => {
30
            obs.next(this.navigateUserToLogin());
31
            obs.complete();
32
          }
33
        );
34
    });
35
  }
36
}