Angular Route Guard: Implement Route Guard in Angular 10

Angular Route Guard: Implement Route Guard in Angular 10

Angular route guard allows us to grant or remove access to certain parts of the navigation. Angular canActivate guard prevent unauthorized access. Let’s perform a practical and see how we can set up a guard for our angular application.

Angular route guard allows us to grant or remove access to certain parts of the navigation. Another route guard, the CanDeactivate guard, even enable you to prevent a user from accidentally leaving a component with unsaved changes.

Why we need Angular guards

To prevent unauthorized access to certain parts of our navigation, use route guards in Angular.

The client-side route guards like this are not meant to be a security feature. They won’t prevent a smart user from figuring out a way to get to the protected routes.

Such security should be implemented on the server-side. So you need to develop the logic for the server-side, and based on the response, we will change the routes.

Route guards are instead meant as a way to improve the UX for your apps.

Types of routing guards

Route guards in Angular can prevent users from navigating to parts of an app without authorization.

There are 4 route guards available in Angular.

  1. CanActivate: It controls if a route can be activated.
  2. CanActivateChild: It controls if children of a route can be activated.
  3. CanLoad: It controls if a route can even be loaded. This becomes useful for lazy-loaded feature modules. They won’t also load if the guard returns false.
  4. CanDeactivate: It controls if the user can leave a route. Note that this guard doesn’t prevent the user from closing the browser tab or navigating to a different address. It only prevents actions from within the application itself.

To use route guards, consider using component-less routes as this facilitates guarding child routes.

How to Create Guard Service in Angular

To create a service for your guard, type the following command.

ng generate guard your-guard-name

In your guard class, implement the guard you want to use. The following example uses CanActivate to guard the route.

In your guard class, implement the guard you want to use. The following example uses CanActivate to guard the route.export class YourGuard implements CanActivate {
  canActivate(
    next: ActivatedRouteSnapshot,
    state: RouterStateSnapshot): boolean {
      // your  logic goes here
  }
}

angular angular 10 angular route

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Install Angular - Angular Environment Setup Process

Install Angular in easy step by step process. Firstly Install Node.js & npm, then Install Angular CLI, Create workspace and Deploy your App.

Angular ngFor: Build an HTML Table with Angular 10 Example

Build an HTML Table Example Dynamically with Angular 10/9 and ngFor. Step 1 - Getting Table Data with Angular 10 Service and HttpClient. This is better done using an Angular 10 service and the HttpClient API, so let's suppose we have the following service: Step 2 - Displaying the Table Using Angular 10/9 ngFor.

Setting Up Angular 10 Proxy Configuration via angular.json

In this quick Angular 10 proxy tutorial, we learn the easy method to set up Angular CLI to register proxy configuration inside the angular json or CLI configuration file. Setting Up Angular 10 Proxy Configuration via angular.json

Road to Angular 10: Angular 10.0.0-rc.2 Fixes a Bug and Skips 10.0.0-rc.1

The final version of Angular 9 was released in February 2020, which means the road to Angular 10 has begun. We are keeping close tabs on the minor versions.

Angular 10 Material Table with Pagination, Filtering, Sorting

Angular material table is an inbuilt component that can be used to create a table where user can searching, sorting, filtering, and paging rows.