In Angular, route guards are used to control navigation between routes. With Role Guards, you can allow or block access based on user roles (for example: admin, user).
Conditional Routing lets you dynamically redirect or block users depending on conditions like authentication, permissions, or application state.
CanActivate – Guard interface that runs before a route activates.admin) can open protected routes./forbidden).A typical role guard in Angular:
CanActivate from @angular/router.AuthService (or similar) to know the current user role.true when navigation is allowed, otherwise redirects and returns false.canActivate property in the route configuration.This service stores and exposes the current user role for the whole application.
// auth.service.ts - manages current user role
import { Injectable } from '@angular/core';
@Injectable({ providedIn: 'root' })
export class AuthService {
private userRole: string = 'user'; // default role for demonstration
getRole(): string {
return this.userRole;
}
isAdmin(): boolean {
return this.userRole === 'admin';
}
setRole(role: string) {
this.userRole = role; // dynamically set the role during login
}
}
The role guard only allows admins to access the protected route and redirects others.
// role.guard.ts - only allows admins to activate certain routes
import { Injectable } from '@angular/core';
import { CanActivate, Router } from '@angular/router';
import { AuthService } from './auth.service';
@Injectable({ providedIn: 'root' })
export class RoleGuard implements CanActivate {
constructor(private auth: AuthService, private router: Router) {}
canActivate(): boolean {
if (this.auth.isAdmin()) {
return true; // user is admin, allow navigation
}
this.router.navigate(['/forbidden']); // not admin, redirect to forbidden page
return false;
}
}
Attach the RoleGuard to your routes using the canActivate property.
// app.routes.ts - protect the dashboard route using RoleGuard
import { Routes } from '@angular/router';
import { DashboardComponent } from './dashboard/dashboard.component';
import { RoleGuard } from './role.guard';
export const routes: Routes = [
{
path: 'dashboard',
component: DashboardComponent,
canActivate: [RoleGuard] // only admins can open this route
},
{
path: 'forbidden',
loadComponent: () =>
import('./forbidden/forbidden.component').then(m => m.ForbiddenComponent) // standalone: true on component
}
];
The login component decides where to navigate after login based on the user role, using conditional routing.
// login.component.ts - navigate differently for admin vs normal user
import { Component } from '@angular/core';
import { Router } from '@angular/router';
import { AuthService } from '../auth.service';
@Component({
selector: 'app-login',
template: `<button (click)="login()">Login</button>`
})
export class LoginComponent {
constructor(private auth: AuthService, private router: Router) {}
login() {
if (this.auth.isAdmin()) {
this.router.navigate(['/dashboard']); // admin goes to dashboard
} else {
this.router.navigate(['/forbidden']); // non-admin goes to forbidden page
}
}
}
/dashboard.RoleGuard because it is listed in canActivate for that route.RoleGuard calls auth.isAdmin() from AuthService.admin, the guard returns true and navigation continues./forbidden.false, so /dashboard is never activated.LoginComponent uses conditional routing:
/dashboard./forbidden.This combination of route guards and conditional navigation helps you protect sensitive pages and provide a better user experience.
AdminGuard, UserGuard) for clarity.ModeratorGuard that only allows moderators to access a /moderation route./profile route that conditionally shows extra admin controls if the user is an admin.AuthService to simulate login/logout and dynamically change roles, then test routing behavior.canMatch guard to prevent entire feature modules from loading for certain roles.