Explain it simply
Route Guards, in plain language
Route Guards are used to control access to routes (authentication, authorization).
Problem → Why → Solution
The problem — why this concept matters
Assuming a client guard secures API data creates an authorization vulnerability.
A common mistake
One happy-path result does not prove the feature works; empty, loading, error, and cleanup behavior need explicit tests.
Core concept — understand how it works
A route guard controls navigation UX or redirects, while the server remains the authority for authorization security. Returning a UrlTree or redirect result is clearer than imperatively triggering navigation. Keep authentication, role, unsaved-change, and asynchronous checks focused and avoid redirect loops.
- URL or navigation
- router config and guard
- component view
Explain the core concept and Angular data flow of Route Guards
Core syntax or pattern
// auth.guard.ts
import { Injectable } from '@angular/core';
import { CanActivate, Router, ActivatedRouteSnapshot } from '@angular/router';
@Injectable({ providedIn: 'root' })
export class AuthGuard implements CanActivate {
constructor(private authService: AuthService, private router: Router) {}
canActivate(route: ActivatedRouteSnapshot): boolean {
if (this.authService.isLoggedIn()) {
return true; // Allow access
}
// Redirect to login
this.router.navigate(['/login']);
return false;
}
}
// app-routing.module.ts
const routes: Routes = [
{
path: 'admin',
component: AdminComponent,
canActivate: [AuthGuard] // Protected route
},
{ path: 'login', component: LoginComponent }
];
// Other guards:
// canDeactivate - prevent leaving a page
// canLoad - prevent lazy loading
// canActivateChild - protect child routesA route guard controls navigation UX or redirects, while the server remains the authority for authorization security. Returning a UrlTree or redirect result is clearer than imperatively triggering navigation. Keep authentication, role, unsaved-change, and asynchronous checks focused and avoid redirect loops.
Read the code step by step
- URL or navigation
// auth.guard.tsRoute Guards are used to control access to routes (authentication, authorization).
- router config and guard
}A route guard controls navigation UX or redirects, while the server remains the authority for authorization security. Returning a UrlTree or redirect result is clearer than imperatively triggering navigation. Keep authentication, role, unsaved-change, and asynchronous checks focused and avoid redirect loops.
- component view
// canActivateChild - protect child routesBuild a protected admin route and an unsaved-edit route, testing logged-out, forbidden, allowed, cancel, and confirm cases.
- URL or navigation
- router config and guard
- component view
Success
Explain the core concept and Angular data flow of Route Guards
Failure
Assuming a client guard secures API data creates an authorization vulnerability.
Route Guards are used to control access to routes (authentication, authorization).
// auth.guard.ts
import { Injectable } from '@angular/core';
import { CanActivate, Router, ActivatedRouteSnapshot } from '@angular/router';
@Injectable({ providedIn: 'root' })
export class AuthGuard implements CanActivate {
constructor(private authService: AuthService, private router: Router) {}
canActivate(route: ActivatedRouteSnapshot): boolean {
if (this.authService.isLoggedIn()) {
return true; // Allow access
}
// Redirect to login
this.router.navigate(['/login']);
return false;
}
}
// app-routing.module.ts
const routes: Routes = [
{
path: 'admin',
component: AdminComponent,
canActivate: [AuthGuard] // Protected route
},
{ path: 'login', component: LoginComponent }
];
// Other guards:
// canDeactivate - prevent leaving a page
// canLoad - prevent lazy loading
// canActivateChild - protect child routes(If the user isn't logged in, they won't be able to access protected routes)Real-world usage
How to apply it in a real project
Build a protected admin route and an unsaved-edit route, testing logged-out, forbidden, allowed, cancel, and confirm cases.
Mini challenge
Build it yourself
Rebuild the Route Guards example without copying it. Add two normal states, one edge case, and one failure state, and write the expected UI and state transition before running it.
- Rebuild the example without copying it
- Test a normal input and an edge case
- Predict the compiler or output result before running it
// auth.guard.ts
import { Injectable } from '@angular/core';
import { CanActivate, Router, ActivatedRouteSnapshot } from '@angular/router';
@Injectable({ providedIn: 'root' })
export class AuthGuard implements CanActivate {
constructor(private authService: AuthService, private router: Router) {}
canActivate(route: ActivatedRouteSnapshot): boolean {
if (this.authService.isLoggedIn()) {
return true; // Allow access
}
// Redirect to login
this.router.navigate(['/login']);
return false;
}
}
// app-routing.module.ts
const routes: Routes = [
{
path: 'admin',
component: AdminComponent,
canActivate: [AuthGuard] // Protected route
},
{ path: 'login', component: LoginComponent }
];
// Other guards:
// canDeactivate - prevent leaving a page
// canLoad - prevent lazy loading
// canActivateChild - protect child routesExpected: Explain the core concept and Angular data flow of Route Guards
Show hint
Build a protected admin route and an unsaved-edit route, testing logged-out, forbidden, allowed, cancel, and confirm cases.
Quick quiz 1 / 3
Quick quiz 2 / 3
Quick quiz 3 / 3
What to remember
- URL or navigation → router config and guard → component view
- A route guard controls navigation UX or redirects, while the server remains the authority for authorization security. Returning a UrlTree or redirect result is clearer than imperatively triggering navigation. Keep authentication, role, unsaved-change, and asynchronous checks focused and avoid redirect loops.
- Assuming a client guard secures API data creates an authorization vulnerability.
- Build a protected admin route and an unsaved-edit route, testing logged-out, forbidden, allowed, cancel, and confirm cases.
Angular — Routing — Angular