Angular Routing Role-Based Access
In Angular, routing guards control whether a user can navigate to a route. When combined with role-based access control and conditional routing, they enable powerful and flexible navigation strategies.
In this topic, you will learn how to use:
/forbidden route.admin, manager)./forbidden.A typical role-based guard relies on an authentication / authorization service that stores the current user's role. The guard uses this information to decide if a route should be accessible or not.
Commonly used guard interfaces in Angular:
CanActivate – Check before navigating to a route.CanLoad – Check before loading a lazy-loaded module.CanActivateChild – Check access to child routes.First, create an AuthService that manages the current user's role and exposes helper methods like isAdmin() and isManager().
auth.service.ts
// Auth service managing user roles for guards
import { Injectable } from '@angular/core';
@Injectable({ providedIn: 'root' })
export class AuthService {
private userRole: string = 'user';
getRole(): string {
return this.userRole;
}
isAdmin(): boolean {
return this.userRole === 'admin';
}
isManager(): boolean {
return this.userRole === 'manager';
}
setRole(role: string) {
this.userRole = role;
}
}
Now create a guard that allows access if the user is either an Admin or a Manager. Otherwise, the user is redirected to the /forbidden route.
multiple-role.guard.ts
// Guard that allows Admin or Manager users
import { Injectable } from '@angular/core';
import { CanActivate, Router } from '@angular/router';
import { AuthService } from './auth.service';
@Injectable({ providedIn: 'root' })
export class MultipleRoleGuard implements CanActivate {
constructor(private auth: AuthService, private router: Router) {}
canActivate(): boolean {
if (this.auth.isAdmin() || this.auth.isManager()) {
return true;
}
// Redirect unauthorized users to a forbidden page
this.router.navigate(['/forbidden']);
return false;
}
}
Multiple guards can be applied to a single route. For example, you may first check if the user is authenticated and then verify their role.
app.routes.ts (Chaining)
// Route configuration with chained guards
import { Routes } from '@angular/router';
import { DashboardComponent } from './dashboard/dashboard.component';
import { MultipleRoleGuard } from './multiple-role.guard';
import { AuthGuard } from './auth.guard';
export const routes: Routes = [
{
path: 'dashboard',
component: DashboardComponent,
canActivate: [AuthGuard, MultipleRoleGuard]
},
{
path: 'forbidden',
loadComponent: () =>
import('./forbidden/forbidden.component').then(m => m.ForbiddenComponent)
}
];
For large applications, it's common to lazy-load feature modules like AdminModule. You can protect these lazy-loaded modules using role-based guards.
// app.routes.ts with lazy-loaded admin module and role guard
import { Routes } from '@angular/router';
import { RoleGuard } from './role.guard';
import { ForbiddenComponent } from './forbidden/forbidden.component';
export const routes: Routes = [
{
path: 'admin',
loadChildren: () =>
import('./admin/admin.module').then(m => m.AdminModule),
canActivate: [RoleGuard]
},
{
path: 'forbidden',
component: ForbiddenComponent
}
];
AuthService (e.g., admin or user)./dashboard or /admin), Angular checks the configured guards.AuthGuard ensures the user is authenticated.MultipleRoleGuard / RoleGuard ensures the user has the required role.true, navigation continues; otherwise, the user is redirected to /forbidden.canLoad to avoid loading a module if the user is not allowed to access it.AuthService) instead of hardcoding roles in multiple guards.VIPGuard that only allows users with role vip to access a /vip-area route.canLoad on a lazy-loaded ReportsModule so only managers can load reports.AuthGuard, MultipleRoleGuard, and a custom SubscriptionGuard for a premium dashboard route./forbidden component that displays a friendly message and a button to go back to a safe page.AuthService to support multiple roles per user (e.g., ['admin', 'editor']) and update your guards accordingly.