Login Register
Code2night
  • Home
  • Blog Archive
  • Learn
    • Tutorials
    • Videos
  • Interview Q&A
  • Languages
    • Angular
    • Angular js
    • Asp.net Core
    • C
    • C#
    • DotNet
    • HTML/CSS
    • Java
    • JavaScript
    • Node.js
    • Python
    • React
    • Security
    • SQL Server
    • TypeScript
  • Post Blog
  • Tools
    • JSON Beautifier
    • HTML Beautifier
    • XML Beautifier
    • CSS Beautifier
    • JS Beautifier
    • PDF Editor
    • Word Counter
    • Base64 Encode/Decode
    • Diff Checker
    • JSON to CSV
    • Password Generator
    • SEO Analyzer
    • Background Remover
  1. Home
  2. Blog
  3. Angular
  4. Mastering Route Guards in Angular: Understanding CanActivate and CanDeactivate

Mastering Route Guards in Angular: Understanding CanActivate and CanDeactivate

Date- Mar 25,2026

1

angular route guards

Overview

Route Guards in Angular serve as a vital mechanism for controlling access to different routes within an application. They function as a gatekeeper, determining whether a user can navigate to a specific route or not. This capability is essential in scenarios where access to certain areas of an application needs to be restricted based on user authentication, roles, or unsaved changes in forms. By leveraging Route Guards, developers can enforce security, improve user experience, and ensure data integrity.

Real-world use cases for Route Guards include scenarios such as protecting admin routes, where only users with administrative privileges should have access, or safeguarding routes containing sensitive data that should only be visible to authenticated users. Additionally, CanDeactivate guards are particularly useful for prompting users about unsaved changes before they navigate away from a route, helping to prevent accidental data loss.

Prerequisites

  • Angular CLI: Familiarity with Angular CLI commands to create and manage Angular applications.
  • TypeScript: Basic understanding of TypeScript as Angular is built with it.
  • Angular Routing: Knowledge of Angular routing concepts and how to set up routes within an Angular application.
  • Services: Understanding of Angular services and dependency injection, as Route Guards often use services for authentication or data fetching.

Understanding CanActivate

The CanActivate interface is used to determine if a user can access a particular route. It allows developers to implement custom logic to check user authentication, roles, or any other conditions before a route is activated. The return value of the CanActivate method can either be a boolean, indicating permission, or an Observable/Promise that resolves to a boolean. This flexibility allows for asynchronous checks, such as verifying a user's authentication status from a server.

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

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

  canActivate(
    next: ActivatedRouteSnapshot,
    state: RouterStateSnapshot
  ): boolean {
    if (this.authService.isAuthenticated()) {
      return true;
    }
    this.router.navigate(['/login']);
    return false;
  }
}

This code implements a simple authentication guard:

  • Import necessary modules: The guard imports Angular's CanActivate interface, ActivatedRouteSnapshot, RouterStateSnapshot, and Router for navigation.
  • Inject services: The constructor injects AuthService for authentication checks and Router for navigation control.
  • Implement canActivate: The canActivate method checks if the user is authenticated. If true, it allows navigation; otherwise, it redirects to the login page and returns false.

The expected output is that authenticated users can access protected routes, while unauthenticated users are redirected to the login page.

Asynchronous CanActivate

In scenarios where authentication checks involve server communication, the CanActivate method can return an Observable or a Promise. This allows the application to wait for the authentication check to complete before deciding on route access. Here's an example of an asynchronous CanActivate guard:

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

@Injectable({ providedIn: 'root' })
export class AsyncAuthGuard implements CanActivate {
  constructor(private authService: AuthService) {}

  canActivate(
    next: ActivatedRouteSnapshot,
    state: RouterStateSnapshot
  ): Observable {
    return this.authService.isAuthenticatedAsync();
  }
}

This implementation leverages an asynchronous method from the AuthService:

  • Observable return type: The canActivate method returns an Observable, which allows the router to handle the asynchronous nature of the authentication check.
  • Service method: The isAuthenticatedAsync method in the AuthService should return an Observable that emits true or false based on authentication status.

Understanding CanDeactivate

The CanDeactivate interface provides a way to prevent users from navigating away from a route if they have unsaved changes. This is particularly important in forms where users might lose data if they accidentally navigate away. The CanDeactivate guard prompts the user with a confirmation dialog before allowing the navigation, ensuring that they are aware of potential data loss.

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

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

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

This code demonstrates how to implement a CanDeactivate guard:

  • Define an interface: The CanComponentDeactivate interface specifies a canDeactivate method that returns an Observable, Promise, or boolean.
  • Implement the guard: The CanDeactivateGuard class implements CanDeactivate and checks if the component has the canDeactivate method. If it does, it invokes it; otherwise, it allows navigation.

When this guard is used, components implementing CanComponentDeactivate will be prompted about unsaved changes before navigating away.

Implementing CanDeactivate in a Component

To utilize the CanDeactivate guard effectively, components must implement the CanComponentDeactivate interface. Here’s an example of a component with unsaved changes:

import { Component } from '@angular/core';
import { CanComponentDeactivate } from './can-deactivate.guard';

@Component({
  selector: 'app-edit-profile',
  templateUrl: './edit-profile.component.html'
})
export class EditProfileComponent implements CanComponentDeactivate {
  hasUnsavedChanges: boolean = false;

  canDeactivate(): boolean {
    if (this.hasUnsavedChanges) {
      return confirm('You have unsaved changes! Do you really want to leave?');
    }
    return true;
  }
}

This component implementation includes:

  • Component setup: The EditProfileComponent implements CanComponentDeactivate to provide the canDeactivate method.
  • Unsaved changes logic: The hasUnsavedChanges property tracks whether the user has made changes. The canDeactivate method prompts the user with a confirmation dialog if there are unsaved changes.

Edge Cases & Gotchas

While Route Guards provide powerful features, developers should be aware of potential pitfalls:

  • Asynchronous checks: If the Observable returned by CanActivate does not complete, the route will not activate. Always ensure that Observables resolve correctly and handle errors appropriately.
  • Multiple guards: When multiple guards are applied to a route, all of them must return true for the route to activate. Be cautious of the order of guards and ensure that they do not conflict with each other.
  • Guard dependencies: Be mindful of injecting services into guards. Circular dependencies can occur if guards depend on each other or services that depend on guards.

Performance & Best Practices

To ensure optimal performance and maintainability when using Route Guards, consider the following best practices:

  • Minimize side effects: Route Guards should focus on determining access rather than performing side effects like data fetching or state mutation. Keep them lightweight to avoid performance bottlenecks.
  • Use observables for async operations: When performing asynchronous checks, leverage observables to manage state and side effects effectively.
  • Centralize guard logic: If multiple routes share similar guard logic, consider creating a centralized guard service to avoid code duplication and enhance maintainability.

Real-World Scenario: Profile Editing Application

Let’s implement a simple profile editing application that utilizes both CanActivate and CanDeactivate guards. This application will demonstrate user authentication and unsaved changes prevention.

import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { AuthGuard } from './auth.guard';
import { CanDeactivateGuard } from './can-deactivate.guard';
import { EditProfileComponent } from './edit-profile/edit-profile.component';
import { LoginComponent } from './login/login.component';

const routes: Routes = [
  { path: 'edit-profile', component: EditProfileComponent, canActivate: [AuthGuard], canDeactivate: [CanDeactivateGuard] },
  { path: 'login', component: LoginComponent },
  { path: '', redirectTo: '/login', pathMatch: 'full' }
];

@NgModule({
  imports: [RouterModule.forRoot(routes)],
  exports: [RouterModule]
})
export class AppRoutingModule {}

In this routing configuration:

  • Edit Profile Route: The edit-profile route is protected by AuthGuard to ensure only authenticated users can access it and by CanDeactivateGuard to handle unsaved changes.
  • Login Route: The login route allows users to authenticate themselves.

Next, we need to implement the AuthService and the EditProfileComponent to complete the functionality.

import { Injectable } from '@angular/core';
import { BehaviorSubject } from 'rxjs';

@Injectable({ providedIn: 'root' })
export class AuthService {
  private isAuthenticatedSubject = new BehaviorSubject(false);

  isAuthenticated(): boolean {
    return this.isAuthenticatedSubject.value;
  }

  logIn() {
    this.isAuthenticatedSubject.next(true);
  }

  logOut() {
    this.isAuthenticatedSubject.next(false);
  }

  isAuthenticatedAsync(): Observable {
    return this.isAuthenticatedSubject.asObservable();
  }
}

This AuthService manages user authentication:

  • BehaviorSubject: The isAuthenticatedSubject holds the authentication state, allowing components to reactively update based on authentication status.
  • LogIn and LogOut methods: These methods toggle the authentication state.

Finally, we incorporate the EditProfileComponent:

import { Component } from '@angular/core';
import { CanComponentDeactivate } from './can-deactivate.guard';

@Component({
  selector: 'app-edit-profile',
  templateUrl: './edit-profile.component.html'
})
export class EditProfileComponent implements CanComponentDeactivate {
  hasUnsavedChanges: boolean = false;

  canDeactivate(): boolean {
    if (this.hasUnsavedChanges) {
      return confirm('You have unsaved changes! Do you really want to leave?');
    }
    return true;
  }
}

This component tracks unsaved changes and prompts the user accordingly.

Conclusion

  • Route Guards in Angular are essential for enforcing route access and preventing data loss.
  • CanActivate is used for controlling access based on conditions such as authentication.
  • CanDeactivate helps prevent accidental data loss by prompting users about unsaved changes.
  • Asynchronous handling in guards is crucial for operations like server authentication checks.
  • Be cautious of edge cases and potential pitfalls when implementing guards.

S
Shubham Saini
Programming author at Code2Night — sharing tutorials on ASP.NET, C#, and more.
View all posts →

Related Articles

Mastering State Management in Angular with NgRx: A Comprehensive Guide
Mar 25, 2026
Mastering Angular Services and Dependency Injection for Scalable Applications
Mar 25, 2026
Mastering HTTP Interceptors in Angular for Enhanced API Communication
Mar 25, 2026
Essential Security Best Practices for .NET 10 Development
Mar 25, 2026
Previous in Angular
Understanding Data Binding in Angular: Mastering One-way and Two-…
Next in Angular
Mastering HTTP Interceptors in Angular for Enhanced API Communica…

Comments

Contents

More in Angular

  • Tag or mention people like WhatsApp & Skype by using @ in An… 10384 views
  • How to Apply css on child components in Angular 10174 views
  • Export to Excel in Angular using ExcelJS? 9753 views
  • Angular Material Select Dropdown with Image 8266 views
  • How To Consume Web API Using Angular 4012 views
View all Angular posts →

Tags

AspNet C# programming AspNet MVC c programming AspNet Core C software development tutorial MVC memory management Paypal coding coding best practices data structures programming tutorial tutorials object oriented programming Slick Slider StripeNet
Free Download for Youtube Subscribers!

First click on Subscribe Now and then subscribe the channel and come back here.
Then Click on "Verify and Download" button for download link

Subscribe Now | 1760
Download
Support Us....!

Please Subscribe to support us

Thank you for Downloading....!

Please Subscribe to support us

Continue with Downloading
Be a Member
Join Us On Whatsapp
Code2Night

A community platform for sharing programming knowledge, tutorials, and blogs. Learn, write, and grow with developers worldwide.

Panipat, Haryana, India
info@code2night.com
Quick Links
  • Home
  • Blog Archive
  • Tutorials
  • About Us
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Guest Posts
  • SEO Analyzer
Free Dev Tools
  • JSON Beautifier
  • HTML Beautifier
  • CSS Beautifier
  • JS Beautifier
  • Password Generator
  • QR Code Generator
  • Hash Generator
  • Diff Checker
  • Base64 Encode/Decode
  • Word Counter
  • SEO Analyzer
By Language
  • Angular
  • Angular js
  • Asp.net Core
  • C
  • C#
  • DotNet
  • HTML/CSS
  • Java
  • JavaScript
  • Node.js
  • Python
  • React
  • Security
  • SQL Server
  • TypeScript
© 2026 Code2Night. All Rights Reserved.
Made with for developers  |  Privacy  ·  Terms
Translate Page
We use cookies to improve your experience and analyze site traffic. By clicking Accept, you consent to our use of cookies. Privacy Policy
Accessibility
Text size
High contrast
Grayscale
Dyslexia font
Highlight links
Pause animations
Large cursor