Files
angular-challenges/libs/module-to-standalone/admin/shared/src/lib/authorized.guard.ts
2023-06-19 18:06:50 +02:00

24 lines
671 B
TypeScript

import { CanActivate, Router, UrlTree } from '@angular/router';
import { AuthorizationService } from '@angular-challenges/module-to-standalone/core/service';
import { Injectable } from '@angular/core';
import { Observable, map } from 'rxjs';
@Injectable({
providedIn: 'root',
})
export class IsAuthorizedGuard implements CanActivate {
constructor(
private authorizationService: AuthorizationService,
private router: Router
) {}
canActivate(): Observable<boolean | UrlTree> {
return this.authorizationService.isAuthorized$.pipe(
map((isAuthorized) =>
isAuthorized ? true : this.router.createUrlTree(['forbidden'])
)
);
}
}