import { Injectable } from '@angular/core' ; import { ActivatedRouteSnapshot , CanActivate , RouterStateSnapshot } from '@angular/router' ; import { Store } from '@ngxs/store' ; import { Observable } from 'rxjs' ; import { map } from 'rxjs/operators' ; import { ProjectUser } from '../../projects/models/project-user' ; import { ProjectUserState } from '../../projects/stores/project-user.state' ; @ Injectable ( { providedIn : 'root' , } ) export class CanGuard implements CanActivate { user$ : Observable < ProjectUser > ; constructor ( private store ) { this . user$ = this . store . select ( ProjectUserState . userSelf ) ; } canActivate ( next , state ) { const roles = ( next . data [ 'roles' ] || [ ] ) as Array < string > ; return this . user$ . pipe ( map ( user => ( user && user . role && roles . findIndex ( r => r === user . role ) >= 0 ) || false ) ) ; } }