import { Injectable } from '@angular/core' ; import { Router , ActivatedRouteSnapshot } from '@angular/router' ; import { KeycloakService , KeycloakAuthGuard } from 'keycloak-angular' ; @ Injectable ( { providedIn : 'root' } ) export class AppAuthGuard extends KeycloakAuthGuard { constructor ( protected router , protected keycloakAngular ) { super ( router , keycloakAngular ) ; } isAccessAllowed ( route ) { return new Promise ( async ( resolve , reject ) => { if ( ! this . authenticated ) { this . keycloakAngular . login ( ) ; return resolve ( true ) ; } const requiredRoles = route . data . roles ; if ( ! requiredRoles || requiredRoles . length === 0 ) { return resolve ( true ) ; } else { if ( ! this . roles || this . roles . length === 0 ) { resolve ( false ) ; } let granted = false ; for ( const requiredRole of requiredRoles ) { if ( this . roles . indexOf ( requiredRole ) > - 1 ) { granted = true ; break ; } } resolve ( granted ) ; } } ) ; } }