Files
zitadel/console/src/app/guards/user.guard.ts
T
133aec4568 chore(console): angular 16 (#5870)
* ng 16

* material cdk

* module imports, rmn @accounts

* deps

* lint

* buf

---------

Co-authored-by: Elio Bischof <[email protected]>
2023-05-18 19:28:35 +00:00

28 lines
790 B
TypeScript

import { Injectable } from '@angular/core';
import { ActivatedRouteSnapshot, Router, RouterStateSnapshot } from '@angular/router';
import { Observable } from 'rxjs';
import { map, tap } from 'rxjs/operators';
import { GrpcAuthService } from '../services/grpc-auth.service';
@Injectable({
providedIn: 'root',
})
export class UserGuard {
constructor(private authService: GrpcAuthService, private router: Router) {}
public canActivate(
route: ActivatedRouteSnapshot,
state: RouterStateSnapshot,
): Observable<boolean> | Promise<boolean> | boolean {
return this.authService.user.pipe(
map((user) => user?.id !== route.params['id']),
tap((isNotMe) => {
if (!isNotMe) {
this.router.navigate(['/users', 'me']);
}
}),
);
}
}