improved canActivate method to redirect and cover all cases
This commit is contained in:
		@@ -7,7 +7,7 @@ import {Router} from '@angular/router';
 | 
			
		||||
// TODO: filter by not completely filled/no measurements
 | 
			
		||||
// TODO: validation of samples
 | 
			
		||||
 | 
			
		||||
// TODO: get rid of chart.js (+moment.js) and lodash
 | 
			
		||||
// TODO: get rid of chart.js (+moment.js)
 | 
			
		||||
 | 
			
		||||
@Component({
 | 
			
		||||
  selector: 'app-root',
 | 
			
		||||
 
 | 
			
		||||
@@ -1,6 +1,6 @@
 | 
			
		||||
import { Injectable } from '@angular/core';
 | 
			
		||||
import {ApiService} from './api.service';
 | 
			
		||||
import {ActivatedRouteSnapshot, CanActivate, RouterStateSnapshot} from '@angular/router';
 | 
			
		||||
import {ActivatedRouteSnapshot, CanActivate, Router, RouterStateSnapshot} from '@angular/router';
 | 
			
		||||
import {LocalStorageService} from 'angular-2-local-storage';
 | 
			
		||||
import {Observable} from 'rxjs';
 | 
			
		||||
 | 
			
		||||
@@ -26,7 +26,8 @@ export class LoginService implements CanActivate {
 | 
			
		||||
 | 
			
		||||
  constructor(
 | 
			
		||||
    private api: ApiService,
 | 
			
		||||
    private storage: LocalStorageService
 | 
			
		||||
    private storage: LocalStorageService,
 | 
			
		||||
    private router: Router
 | 
			
		||||
  ) {
 | 
			
		||||
 | 
			
		||||
  }
 | 
			
		||||
@@ -79,23 +80,24 @@ export class LoginService implements CanActivate {
 | 
			
		||||
 | 
			
		||||
  canActivate(route: ActivatedRouteSnapshot = null, state: RouterStateSnapshot = null): Observable<boolean> {
 | 
			
		||||
    return new Observable<boolean>(observer => {
 | 
			
		||||
      const pathPermission = this.pathPermissions.find(e => e.path.indexOf(route.url[0].path) >= 0);
 | 
			
		||||
      if (!pathPermission || this.is(pathPermission.permission)) {  // check if level is permitted for path
 | 
			
		||||
      new Promise(resolve => {
 | 
			
		||||
        if (this.loggedIn === undefined) {
 | 
			
		||||
          this.login().then(res => {
 | 
			
		||||
            observer.next(res as any);
 | 
			
		||||
            observer.complete();
 | 
			
		||||
            resolve(res);
 | 
			
		||||
          });
 | 
			
		||||
        }
 | 
			
		||||
        else {
 | 
			
		||||
          observer.next(this.loggedIn);
 | 
			
		||||
          observer.complete();
 | 
			
		||||
          resolve(this.loggedIn);
 | 
			
		||||
        }
 | 
			
		||||
      }
 | 
			
		||||
      else {
 | 
			
		||||
        observer.next(false);
 | 
			
		||||
      }).then(res => {
 | 
			
		||||
        const pathPermission = this.pathPermissions.find(e => e.path.indexOf(route.url[0].path) >= 0);
 | 
			
		||||
        const ok = res && !pathPermission || this.is(pathPermission.permission); // check if level is permitted for path
 | 
			
		||||
        observer.next(ok);
 | 
			
		||||
        observer.complete();
 | 
			
		||||
      }
 | 
			
		||||
        if (!ok) {
 | 
			
		||||
          this.router.navigate(['/']);
 | 
			
		||||
        }
 | 
			
		||||
      });
 | 
			
		||||
    });
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user