Skip to solution
mediumFrontend

What are functional Route Guards, and how do they differ from class-based guards?

543 views
01

Understand the problem

Explain functional route guards in modern Angular.

routingguards
02

Attempt it yourself

Sketch your approach before reading the solution — that's what interviews test.

Nudge consolestandby

Stuck? Beam a request up — the console returns a conceptual nudge that guides your logic without spoiling the implementation.

03

Study the solution

Functional route guards are standard JavaScript functions registered as guards (e.g., return boolean or UrlTree). They utilize inject() to fetch dependencies, avoiding the boilerplate of class-based guards implementing interfaces, and are simpler to combine and reuse.

Solution ready — 2 min read

Classified // press E to declassify

04

Read the code

Functional route guard with inject()
import { inject } from '@angular/core';
import { Router, CanActivateFn } from '@angular/router';
import { AuthService } from './auth.service';

export const authGuard: CanActivateFn = (route, state) => {
  const authService = inject(AuthService);
  const router = inject(Router);

  if (authService.isAuthenticated()) {
    return true;
  }
  
  return router.parseUrl('/login');
};
05

Join the discussion

Discussion (0)

Sign in to join the discussion.

No responses yet. Be the first to share what you think.

Transmission complete // awaiting log

KEEP THE
STREAK ALIVE.

Dossier 49 of 121 decoded in the Angular track. One more won't hurt.

Back to track