Sub topic 16
Why guards are not a security boundary
This chapter assumes you can write a functional canActivate guard, return a UrlTree, and read a resolver's data in a component. Here you will see what a guard protects and what it does not.
Where the code runs
Angular guards run in the browser. The user controls the browser. They can open developer tools, edit JavaScript, change localStorage, set breakpoints, or skip your application and send HTTP requests directly with curl or another tool.
A guard that returns false or a redirect only changes what your app shows. It does not stop anyone from calling your API.
Do not rely on the diagram syntax above. The next diagram is the one for this chapter.
What guards are good for
Guards improve the user experience. They:
- Send anonymous users to the login page instead of showing a broken screen.
- Hide pages a user cannot use anyway.
- Warn about unsaved changes.
- Skip downloading lazy-loaded code with
canMatch.
Treat all of this as convenience, not protection.
The code is not secret
canMatch stops the router from downloading an admin chunk for non-admins. That does not make the chunk private. Anyone can request the JavaScript file by its URL. Never put secrets, API keys, or sensitive business rules in front-end code, even in a lazy-loaded bundle.
Where the real checks belong
Enforce authorization on the server for every request:
- Authenticate the request, for example by validating a session cookie or a token.
- Check that this user may perform this action on this resource.
- Return
401 Unauthorizedor403 Forbiddenwhen the check fails.
The client can then react to those responses. An HTTP interceptor can catch a 401 and send the user to the login page:
import { inject } from '@angular/core';
import { HttpErrorResponse, HttpInterceptorFn } from '@angular/common/http';
import { Router } from '@angular/router';
import { catchError, throwError } from 'rxjs';
export const unauthorizedInterceptor: HttpInterceptorFn = (req, next) => {
const router = inject(Router);
return next(req).pipe(
catchError((error: HttpErrorResponse) => {
if (error.status === 401) {
router.navigate(['/login']);
}
return throwError(() => error);
}),
);
};Register it with provideHttpClient(withInterceptors([unauthorizedInterceptor])). The server stays the authority, and the interceptor only handles the user experience when the server says no.
Summary of responsibilities
| Concern | Guard (client) | Server |
|---|---|---|
| Hide a page from the wrong user | Yes | Not needed |
| Block access to data | No | Yes |
| Prevent loss of unsaved edits | Yes | Not applicable |
| Keep secrets private | No | Yes |
Exercise
A teammate says, "Our admin page is safe because the canActivate guard checks isAdmin()." Give two reasons this is wrong, and say what to add.
Show answer
The guard runs in the browser, so a user can alter the client-side state it reads or modify the code. The user can also call the admin API directly and never pass through the router. Add server-side authentication and authorization checks to every admin endpoint, and keep the guard only as a user-experience layer.