Hi Angular Developers,
Please I need your help, I need to block routing with specific roles, This is my Doc with profiles:
My angular guards return and object with profiles: {admin: true, current: false}
import { Injectable } from '@angular/core';
import { CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot, Router } from '@angular/router';
import { Observable } from 'rxjs';
import { AuthService } from '../services/auth.service';
import { map } from 'rxjs/operators';
@Injectable({
providedIn: 'root'
})
export class RoleGuard implements CanActivate {
constructor(
public authService: AuthService,
public router: Router
) { }
canActivate(
next: ActivatedRouteSnapshot,
state: RouterStateSnapshot): Observable<any> | boolean {
return this.authService.getAuthWithProfile().pipe(
map((data) => {
const roles = data.roles;
return roles; // This returns {admin: true, current: false}
})
);
}
}
The question is How implement the guard for Roles in the Angular Routing, for example:
{ path: 'tasks', component: TasksComponent, canActivate: [RoleGuard] },