allerta-vvf/frontend/src/app/_guards/authorize.guard.ts

26 lines
847 B
TypeScript
Raw Normal View History

2021-12-24 15:21:22 +01:00
import { Injectable } from '@angular/core';
import { ActivatedRouteSnapshot, CanActivate, Router, RouterStateSnapshot, UrlTree } from '@angular/router';
import { Observable } from 'rxjs';
import { AuthService } from '../_services/auth.service';
@Injectable({
providedIn: 'root'
})
export class AuthorizeGuard implements CanActivate {
constructor(private authService: AuthService, private router: Router) {
}
canActivate(
route: ActivatedRouteSnapshot,
state: RouterStateSnapshot
): Observable<boolean | UrlTree> | Promise<boolean | UrlTree> | boolean | UrlTree {
console.log(this.authService, route, state);
if(this.authService.profile === undefined) {
console.log("not logged in");
this.router.navigate(['login', state.url.replace('/', '')]);
return false;
} else {
return true;
}
}
}