I have an application that I have installed using Guard Authentication Guard to make sure that users cannot access the application if they are not logged in, for example
import { Injectable } from '@angular/core'; import { CanActivate, Router, ActivatedRouteSnapshot, RouterStateSnapshot, CanActivateChild } from '@angular/router'; import { AuthContext } from './auth-context.service'; @Injectable() export class AuthGuard implements CanActivate { constructor(private router: Router, private authContext: AuthContext) { } canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean {
I want to add another guard for authorization, which will check whether the user is in a specific role. I am currently hiding the link in navigation based on this role.
<div *ngIf="userInRole('Admin')"> This is secret stuff </div>
But if the user knows the route, he can simply connect it to the URL. How can I add the function "userInRole ()" to the Guard? I would need to pass the role name and make a verification code. Do the guards help the parameters?
source share