DEV Community

Cover image for Ultimate Angular Guards & Route Protection Guide
Md. Maruf Rahman
Md. Maruf Rahman

Posted on • Edited on • Originally published at practicaldev.online

Ultimate Angular Guards & Route Protection Guide

Building a secure Angular application means more than just hiding UI elements. I've seen applications where routes were "protected" by hiding navigation links, but users could still access protected routes by typing URLs directly. That's not securityโ€”that's security theater. Route Guards are Angular's way of actually protecting routes at the framework level.

Route Guards are interfaces that control navigation to and from routes. They can check authentication status, verify user permissions, prevent navigation when there are unsaved changes, and even prefetch data before a route activates. They're essential for building secure, user-friendly Angular applications.

What are Angular Route Guards?

Angular Route Guards provide:

  • CanActivate - Control access to routes (authentication/authorization)
  • CanDeactivate - Prevent navigation away from routes (unsaved changes)
  • CanLoad - Prevent lazy-loaded modules from loading
  • Resolve - Prefetch data before route activation
  • Security - Framework-level route protection
  • User Experience - Prevent data loss and improve performance

CanActivate Guard

Protect routes with authentication and authorization:

import { Injectable } from '@angular/core';
import { CanActivate, Router, ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router';
import { AuthService } from '../auth/auth.service';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';

@Injectable({
  providedIn: 'root'
})
export class AuthGuard implements CanActivate {
  constructor(
    private authService: AuthService,
    private router: Router
  ) {}

  canActivate(
    route: ActivatedRouteSnapshot,
    state: RouterStateSnapshot
  ): Observable<boolean> | Promise<boolean> | boolean {
    return this.authService.isAuthenticated.pipe(
      map(isAuthenticated => {
        if (!isAuthenticated) {
          localStorage.setItem('returnUrl', state.url);
          this.authService.Logout();
          this.router.navigate(['/login']);
          return false;
        }
        return true;
      })
    );
  }
}

// Use in routes
const routes: Routes = [
  {
    path: 'business',
    canActivate: [AuthGuard],
    loadChildren: () => import('./business/business.module').then(m => m.BusinessModule)
  }
];
Enter fullscreen mode Exit fullscreen mode

CanDeactivate Guard

Prevent navigation with unsaved changes:

import { Injectable } from '@angular/core';
import { CanDeactivate } from '@angular/router';
import { Observable } from 'rxjs';

export interface CanComponentDeactivate {
  canDeactivate: () => Observable<boolean> | Promise<boolean> | boolean;
}

@Injectable({
  providedIn: 'root'
})
export class CanDeactivateGuard implements CanDeactivate<CanComponentDeactivate> {
  canDeactivate(
    component: CanComponentDeactivate
  ): Observable<boolean> | Promise<boolean> | boolean {
    return component.canDeactivate ? component.canDeactivate() : true;
  }
}

// Component implementation
export class BusinessFormComponent implements CanComponentDeactivate {
  form: FormGroup;
  isDirty: boolean = false;

  canDeactivate(): boolean {
    if (this.form.dirty && !this.isDirty) {
      return confirm('You have unsaved changes. Do you want to leave?');
    }
    return true;
  }
}
Enter fullscreen mode Exit fullscreen mode

Best Practices

  1. Use CanActivate for authentication/authorization
  2. Use CanDeactivate for unsaved changes
  3. Use CanLoad for lazy-loaded modules
  4. Use Resolve to prefetch data
  5. Implement proper error handling
  6. Store return URLs for redirects

๐Ÿ“– Read the Complete Guide

This is just a brief overview! The complete guide on my blog includes:

  • โœ… Role-Based Guards - Authorization with user roles
  • โœ… CanLoad Guard - Prevent lazy module loading
  • โœ… Resolve Guard - Prefetch data before route activation
  • โœ… Multiple Guards - Combining guards for complex scenarios
  • โœ… Guard Testing - Complete testing strategies
  • โœ… Advanced Patterns - Custom guard implementations
  • โœ… Real-world examples from production applications

๐Ÿ‘‰ Read the full article with all code examples here


What's your experience with Angular Guards? Share your tips in the comments! ๐Ÿš€

For more Angular guides, check out my blog covering Routing, Services, HTTP Client, and more.

Top comments (0)