canMatch + Wildcard For Route Access
Route access guards, for logged-in and logged-out gating, use canMatch with a wildcard at the bottom of the route table.
Why
A user who may not enter a route still lands on the right page.
How To Apply
- Use CanMatch in favor of CanActivate in order to allow multiple root redirects.
- Pair with
{ path: '**', redirectTo: '/' }wildcard at the bottom of the route table. - Guards stay route-agnostic in the core layer of App-Shield - no
Router, no path strings, noUrlTree. - The route table in the app layer owns all redirect targets.
- Structure
appRoutesas parallelcanMatch-gated subtrees (logged-in / logged-out), ending with the wildcard. - A
CanActivateFnreturningfalsecancels the in-flight navigation in place: no redirect, no retry, the URL just stops. - Returning a
UrlTreeworks but forces the guard to know the redirect target, which leaks app routing concerns into core. - With
canMatch, afalseskips the route definition during matching and Angular continues to the next match. The**wildcard catches anything that fell through and bounces back to root, where matching re-runs and lands on the correct subtree.