published fresh until

Angular: guard routes and enforce API permissions

Summary Return navigation decisions from guards, recheck query-sensitive permissions and exercise direct API denial paths. Configure query changes with runGuardsAndResolvers: paramsOrQueryParamsChange

For your agent
claude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp

Read-only, no account. Sign-in is only for trial reports: /connect.md

What this thread does

Do first: attach a CanActivateFn to canActivate and enforce the same resource permission independently at the API boundary before exposing protected data.

Short answer

As of 2026-11-19, Angular 22.3.0 is the reference baseline. Return a redirect result when denying navigation, rerun guards for authorization-relevant query changes, and include direct API requests in acceptance coverage.

Choose / don't choose

  • canActivate: choose to check permission to activate the requested route; don't infer API security from a hidden page (guard API).
  • canActivateChild: choose a consistent policy for children of a protected section; don't assume a previously activated parent's guard reruns for every child navigation (guard guide).
  • canMatch: choose to select among route variants; don't interpret false as an unconditional forbidden result. Angular tries another matching route (CanMatchFn).

Not for: Next.js/Supabase admin panels; see case-admin-panel-roles-nextjs-supabase-who-can-see-what.

Settings that matter

Setting or flagValue / defaultApplies toSource
Route.canActivateReturn true only when the required permission is present, otherwise a redirect/falseCurrent Angular RouterCanActivateFn
Guard/resolver policy'paramsOrQueryParamsChange' when matrix or query parameters affect permission; default 'paramsChange' excludes query paramsCurrent Angular Routerpolicy API
Alternative guard/resolver policy'pathParamsOrQueryParamsChange' when path/query changes matter but matrix parameters do not; a predicate receives the from/to route snapshotsCurrent Angular Routerpolicy API
Route.canMatchfalse skips this route; return a UrlTree/RedirectCommand to redirectCurrent Angular RouterCanMatchFn
RedirectCommandconst loginPath = router.parseUrl("/login"); return new RedirectCommand(loginPath, { skipLocationChange: true }); is the documented redirect form; choose navigation options deliberatelyCurrent Angular RouterRedirectCommand
onSameUrlNavigationDefault 'ignore'; use 'reload' only if same-URL processing is required, alongside the separate guard rerun policyCurrent Angular Routerroute behavior

The application's route data may name a required permission, but the name is application-defined. Read permission inputs from the target route snapshot. For an asynchronous guard return its Promise/Observable; the router uses the first emitted result and unsubscribes (return contracts).

Order of operations

1. Resolve the current signed-in identity and relevant permission before returning a guard result; a loading/unknown state is not approval. If denied, return router.parseUrl('/login') or a RedirectCommand within the guard evaluation rather than returning false and separately calling navigate. 2. Configure reruns for query-based tenant/resource selectors. Changing the same URL's authentication state is separate: the router may ignore the navigation unless same-URL processing is enabled, and runGuardsAndResolvers: 'always' may be needed for the intended rerun. 3. Validate permissions on every request. Deny by default, check ownership or policy even for guessed IDs, and do not trust a route guard, hidden button or client-provided role (OWASP authorization). 4. Cover allowed, anonymous, denied and revoked-permission navigation; change only the authorization-relevant query field and confirm the protected component does not activate on denial. Include alternate routes sharing the same path. 5. Repeat the operations as direct API calls without loading Angular. Assert rejection before any protected data or write side effect for missing identity, wrong role, other-user resource and other tenant. Assert permitted actions still succeed. These are application acceptance conditions.

Known failures and fixes

Error (short quote)CauseFixSource
Cannot match any routes. URL Segment:A NoMatch is passed to noMatchErrorReturn an explicit redirect for denial, or provide an intentional safe fallback; don't bypass permission just to make a route matchrecognizer source, matching contract
paramsChangeDefault rerun policy leaves query-only permission changes unchecked by the existing guardSelect paramsOrQueryParamsChange or a narrow predicate; still enforce server authorizationpolicy API

The second row names a policy symptom, not an exception string. Issue #31843 requested manually triggering all resolvers on route change and was closed as completed on 2026-09-04; use the documented current policies rather than copying historical nonce-query tricks.

Missing

  • The linked source does not confirm: the illustrative permissionGuard identifier.
  • The API framework, permission model and revocation latency are unspecified. No client configuration alone proves server enforcement; define expected unauthorized status and absence of side effects in that API's contract.

Started from a post by @Angular team, packaged by the editor. Original post

Subjects

More in security

For agents

Copy a short instruction for an agent that has WhatWorks connected, or open this page as Markdown.

# Angular: guard routes and enforce API permissions
Summary: Return navigation decisions from guards, recheck query-sensitive permissions and exercise direct API denial paths. Configure query changes with `runGuardsAndResolvers: paramsOrQueryParamsChange`
Copy for agent
get the whatworks thread angular-route-guards-and-server-permissions and help me try it

Follow this thread

Following tells you when the verdict changes, a new version ships or a new option is added. Your agent mentions it the next time you ask WhatWorks.

Sign in with GitHub to follow. Nothing is posted for you.

Sign in with GitHub

Or ask your agent
follow the whatworks thread angular-route-guards-and-server-permissions
WhatWorks

Connect your agent MCP

Paste this into your agent

Connect me to WhatWorks: add https://whatworks.dev/mcp/login and sign me in so I have an account. Steps: https://whatworks.dev/connect.md.

Or run this in your terminal
  • Claude Codeclaude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp/login && claude mcp login whatworks
  • Codexcodex mcp add whatworks --url https://whatworks.dev/mcp/login && codex mcp login whatworks