πŸ”’πŸ›‘οΈ Demystifying Angular 16 Route Guard πŸ›‘οΈπŸ”’

Tadit DashTadit Dash
3 min read

Welcome back, fellow Angular enthusiasts! Today, we're diving into the intriguing world of Angular 16 Route Guards. πŸš€

πŸ’‘ IntroductionπŸ’‘

Angular 16 Route Guards are powerful tools that help us control navigation and access to specific routes in our Angular 16 applications. With these guards, we can protect certain routes from unauthorized access or perform additional checks before allowing users to proceed. One such guard we'll explore today is the ProductDetailGuard. Let's unravel its secrets! πŸ•΅οΈβ€β™‚οΈ

πŸ” Background πŸ”

My use case is very simple and it mimics a real application use case. Here it is.

  • ProductDetail Component will only load if the URL contains a ProductId > 0

  • Anything else will be rejected and redirected to Product Component

Let's explore the code!

πŸ” The ProductDetailGuard Code πŸ”

import { inject } from '@angular/core';
import { CanActivateFn, Router } from '@angular/router';

export const ProductDetailGuard: CanActivateFn = (route, state) => {
  return Number(route.paramMap.get('id')) > 0
    ? true
    : inject(Router).createUrlTree(['/products']);
};

In the code snippet above, we define the ProductDetailGuard as a CanActivateFn. This type of guard is responsible for determining whether a user can activate (access) a particular route.

The guard function receives two parameters: route and state. The route parameter contains information about the current route, while state provides details about the router state.

🚧 Guarding the Product Detail Route 🚧

The purpose of our ProductDetailGuard is to protect the route that displays detailed information about a product. It ensures that the user can only access the route if the ProductId provided in the route parameters is a positive number (Number(route.paramMap.get('id')) > 0).

If the condition is met, the guard returns true, allowing the user to proceed to the desired route.

β›” Redirecting Unauthorized Users β›”

On the other hand, if the condition evaluates to false, indicating an invalid or unauthorized access attempt, the guard takes action.

It uses the inject function from @angular/core to access the Angular injector and retrieve an instance of the Router.

With the Router instance in hand, it creates a URL tree using createUrlTree(['/products']). This URL tree represents the desired destination, in this case, the /products route.

By returning the URL tree, the guard effectively redirects unauthorized users back to the products listing page.

πŸ›‘οΈ Putting the Guard into Action πŸ›‘οΈ

To activate our ProductDetailGuard, we need to integrate it into our application's routing configuration. Within the route definition for the product detail route, we can assign the guard using the canActivate property. For example:

{
  path: 'products/:id',
  component: ProductDetailComponent,
  canActivate: [ProductDetailGuard]
}

By attaching the ProductDetailGuard to the canActivate property, we ensure that the guard is invoked before allowing access to the ProductDetailComponent.

This way, we add an extra layer of security to our application, safeguarding the product detail page from unauthorized users.

πŸ”’πŸ”“ Strengthen Your Angular Application with Route Guards πŸ”“πŸ”’

Route guards are an essential part of building secure and robust Angular applications. With guards like the ProductDetailGuard, we can control access to routes, protect sensitive information, and guide users towards the appropriate sections of our application.

By leveraging these powerful tools, we enhance the user experience and ensure that our application remains reliable and trustworthy.

So, next time you find yourself needing to restrict access to certain routes or perform additional checks before allowing navigation, remember to employ Angular 16 Route Guards. Your application and your users will thank you! πŸ’ͺ

πŸ“πŸ—£οΈ Feedback! πŸ—£οΈπŸ“

I would greatly appreciate your feedback on this blog post! Please take a moment to share your thoughts, suggestions, or any questions you may have in the comments below. Your feedback is incredibly valuable to me as it helps me improve and provide you with even better content in the future. Thank you for your support and for taking the time to share your input!

0
Subscribe to my newsletter

Read articles from Tadit Dash directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Tadit Dash
Tadit Dash

As a πŸš€ Vice President with over 12 years of experience, I am a seasoned software architect known for designing and leading teams of engineers to deliver high-quality software products promptly and within budget. Throughout my career, I have spearheaded the end-to-end design of 7 innovative software products 🎯. From conceptualization to deployment planning, I have successfully guided teams through requirements gathering, prototyping, testing, and deployment phases, ensuring exceptional outcomes. I take pride in my industry recognition, including being honored as a πŸ’Ž Microsoft Most Valuable Professional, πŸ’‘ CodeProject Most Valuable Professional, and πŸ† DZone Most Valuable Blogger. Additionally, my expertise has been acknowledged by BookAuthority, which recognized my books on ASP.NET, REST API, Vue.js, and Dependency Injection as the πŸ“š best of all time. In addition to my professional achievements, I am passionate about mentorship and have been privileged to serve as a 🌟 Young Mentor at IndiaMentor, guiding aspiring professionals in their career journeys. For further information about my work and insights, please visit my website at 🌐 http://taditdash.com. You can also connect with me on 🐦 Twitter at https://twitter.com/taditdash, πŸ‘ Facebook at https://www.facebook.com/taditdash, and πŸ’Ό LinkedIn at https://www.linkedin.com/in/taditdash. I am always open to networking and discussing opportunities, so feel free to reach out and connect. Let's explore how we can collaborate and drive innovation in the ever-evolving world of software architecture and development.