Functional router guards in Angular 15 open the door to happier code

How to seamlessly migrate deprecated class guards into functional ones

Kate Gable (former Sky)
HeroDevs
Published in
2 min readMar 6, 2023
A door opens the route guard to Angular

The class-based implementation of guards has been marked as @deprecated in Angular documentation.

Functional router guards replace the class-based approach.

Mark from the Angular team notes three highlights about this feature:

  1. You can now write guards and resolvers as JavaScript functions
  2. Less boilerplate, more flexibility
  3. Inject dependencies

I designed a demo Angular application using version 15 that includes a small number of routes to allow for easy navigation between pages.

Come explore in both main branch on GitHub: https://github.com/katesky/functional-router-guards

as well as in the new implementation branch: https://github.com/katesky/functional-router-guards/tree/convert-to-inject

https://github.com/katesky/functional-router-guards/tree/convert-to-inline-function

The Demo

The Account page is protected by a route guard which only allows access if the canActivate function returns true. If a user attempts to access the Account page without being authenticated, they will be redirected to the Login page.

The Login page includes a login button that allows users to enter their credentials and authenticate. Upon successful authentication, users are redirected back to the Account page.

Users who are already authenticated and are on the Account page can log out by clicking the logout button. This will reset the login status and require users to authenticate again if they attempt to access the Account page.

Continue Reading on HeroDevs Blog: For the full article and more in-depth insights, visit our official blog. Click here to read the complete post on HeroDevs.

About HeroDevs

HeroDevs is a software engineering and consulting studio that specializes in frontend development. Our team has authored or co-authored projects like the Angular CLI, Angular Universal, Scully, XLTS — extended long-term support for AngularJS, Vue2, and many others. We work with fast-growing startups and some of the world’s largest companies like Google, GE, Capital One, Experian, T-Mobile, Corteva, and others. Learn more about us at herodevs.com

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Published in HeroDevs

HeroDevs gives companies never-ending support packages and expert consulting and engineering for sunsetted open source packages

Written by Kate Gable (former Sky)

sharing knowledge, encouraging to learn, promoting passion for coding, supporting mothers who code

Responses (5)

Write a response

That’s a great question! Will reply with code!

OK, but how should one test those functional guards?

2

Great article!

1