Angular Route Guards
Learn how Angular Route Guards control navigation and protect application routes based on conditions such as authentication, permissions and unsaved changes.
What are Route Guards?
Route Guards are used to control whether a user can enter, leave or access a particular Angular route.
A guard can check a condition before navigation is completed. For example, an application can allow only logged-in users to open an admin dashboard.
Guards are commonly used for authentication, authorization and navigation control.
Route Guard Flow
Common Route Guard Types
CanActivate
Controls whether a user can activate a route.
CanActivateChild
Controls access to child routes.
CanDeactivate
Controls whether a user can leave a route.
CanMatch
Controls whether a route can be matched.
CanActivate Guard
CanActivate is used when we want to control whether a route can be activated.
A common example is protecting a dashboard so that only authenticated users can access it.
import { Injectable } from '@angular/core';
import { CanActivate, Router } from '@angular/router';
@Injectable({
providedIn: 'root'
})
export class AuthGuard implements CanActivate {
constructor(
private router: Router
) {
}
canActivate(): boolean {
const isLoggedIn = true;
if (isLoggedIn) {
return true;
}
this.router.navigate(['/login']);
return false;
}
}
Applying CanActivate
const routes: Routes = [
{
path: 'dashboard',
component: DashboardComponent,
canActivate: [AuthGuard]
}
];
Navigation Decision
User logged in: Yes
Guard result: Allowed
Dashboard route can be opened.
Authentication Example
const routes: Routes = [
{
path: 'login',
component: LoginComponent
},
{
path: 'dashboard',
component: DashboardComponent,
canActivate: [AuthGuard]
},
{
path: 'admin',
component: AdminComponent,
canActivate: [AuthGuard]
}
];
In this example, the login route is public, while the dashboard and admin routes are protected.
The guard decides whether navigation should continue.
CanActivateChild Guard
CanActivateChild is useful when a parent route contains multiple child routes that should all follow the same access rule.
This avoids repeating the same guard configuration for every child route.
import { Injectable } from '@angular/core';
import { CanActivateChild } from '@angular/router';
@Injectable({
providedIn: 'root'
})
export class CourseGuard implements CanActivateChild {
canActivateChild(): boolean {
return true;
}
}
const routes: Routes = [
{
path: 'courses',
component: CoursesComponent,
canActivateChild: [CourseGuard],
children: [
{
path: 'overview',
component: OverviewComponent
},
{
path: 'topics',
component: TopicsComponent
}
]
}
];
CanDeactivate Guard
CanDeactivate is used when the application needs to decide whether the user can leave the current route.
A common example is preventing a user from leaving a form page when unsaved changes are present.
export interface CanExitPage {
canExit(): boolean;
}
import { Injectable } from '@angular/core';
import { CanDeactivate } from '@angular/router';
@Injectable({
providedIn: 'root'
})
export class UnsavedChangesGuard
implements CanDeactivate<CanExitPage> {
canDeactivate(
component: CanExitPage
): boolean {
return component.canExit();
}
}
CanDeactivate Form Example
export class EnquiryComponent
implements CanExitPage {
hasUnsavedChanges = true;
canExit(): boolean {
if (this.hasUnsavedChanges) {
return confirm(
'You have unsaved changes. Leave this page?'
);
}
return true;
}
}
const routes: Routes = [
{
path: 'enquiry',
component: EnquiryComponent,
canDeactivate: [UnsavedChangesGuard]
}
];
CIIT Training Institute Enquiry
The user starts filling an enquiry form.
User tries to leave: Guard checks unsaved changes
The application can ask the user for confirmation before leaving the page.
CanMatch Guard
CanMatch can control whether Angular should match a route during route matching.
It can be useful when different users should see different route configurations or when a route should not be selected under a particular condition.
import { Injectable } from '@angular/core';
import { CanMatch, Route, UrlSegment } from '@angular/router';
@Injectable({
providedIn: 'root'
})
export class RoleGuard implements CanMatch {
canMatch(
route: Route,
segments: UrlSegment[]
): boolean {
const isAdmin = true;
return isAdmin;
}
}
const routes: Routes = [
{
path: 'admin',
component: AdminComponent,
canMatch: [RoleGuard]
}
];
Guard Decision
CIIT Training Institute Example
Imagine a CIIT Training Institute admin area where only authorized users can manage courses and enquiries.
The route guard checks the user's access before opening the protected pages.
const routes: Routes = [
{
path: 'admin',
component: AdminComponent,
canActivate: [AuthGuard],
children: [
{
path: 'courses',
component: CourseManagementComponent
},
{
path: 'enquiries',
component: EnquiryManagementComponent
},
{
path: 'reports',
component: ReportsComponent
}
]
}
];
Protected Area
/admin/courses → Course Management
/admin/enquiries → Enquiry Management
/admin/reports → Reports
Redirecting Unauthorized Users
A guard can redirect an unauthorized user to another route, such as a login page or access-denied page.
This allows the application to provide a controlled navigation experience.
canActivate(): boolean {
const isLoggedIn = false;
if (isLoggedIn) {
return true;
}
this.router.navigate(['/login']);
return false;
}
Route Guard Comparison
| Guard | Purpose | Typical Use |
|---|---|---|
CanActivate
|
Controls route activation | Authentication / Authorization |
CanActivateChild
|
Controls child route activation | Protected child pages |
CanDeactivate
|
Controls leaving a route | Unsaved forms |
CanMatch
|
Controls route matching | Conditional route matching |
Important Points
Use route guards when access to a route should depend on authentication or another condition.
CanActivate checks whether a route can be activated.
CanDeactivate is useful when the user may need confirmation before leaving a page.
Guards should focus on navigation decisions rather than containing unnecessary application logic.
Route Guard Best Practices
- Use guards for authentication and authorization requirements.
- Keep guard logic focused on making navigation decisions.
-
Use
CanDeactivatewhen unsaved changes need protection. -
Use
CanActivateChildfor groups of protected child routes. - Redirect users to an appropriate route when access is denied.
- Do not depend on client-side guards alone as the security boundary for backend resources.
Summary
Angular Route Guards control navigation based on conditions such as authentication, authorization and unsaved changes.
Common guards include CanActivate, CanActivateChild, CanDeactivate and CanMatch.
Guards help create controlled navigation flows and protect application routes from unauthorized access.