From 72ecdad5735898d5bc04ac154a630d271e0d3a2a Mon Sep 17 00:00:00 2001 From: VLE2FE Date: Thu, 30 Jul 2020 15:35:19 +0200 Subject: [PATCH] improved canActivate method to redirect and cover all cases --- cf_config/nginx.conf | 9 --------- src/app/app.component.ts | 2 +- src/app/services/login.service.ts | 26 ++++++++++++++------------ 3 files changed, 15 insertions(+), 22 deletions(-) delete mode 100644 cf_config/nginx.conf diff --git a/cf_config/nginx.conf b/cf_config/nginx.conf deleted file mode 100644 index 841957f..0000000 --- a/cf_config/nginx.conf +++ /dev/null @@ -1,9 +0,0 @@ -gzip on; -gzip_disable "msie6"; - -gzip_vary on; -gzip_proxied any; -gzip_comp_level 6; -gzip_buffers 16 8k; -gzip_http_version 1.1; -gzip_types text/plain text/css application/json application/javascript application/x-javascript text/xml application/xml application/xml+rss text/javascript; diff --git a/src/app/app.component.ts b/src/app/app.component.ts index 22ec18b..45633aa 100644 --- a/src/app/app.component.ts +++ b/src/app/app.component.ts @@ -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', diff --git a/src/app/services/login.service.ts b/src/app/services/login.service.ts index f4816bc..fd22687 100644 --- a/src/app/services/login.service.ts +++ b/src/app/services/login.service.ts @@ -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 { return new Observable(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(['/']); + } + }); }); }