[Angular] RoleGuard: redirect with RedirectCommand/UrlTree instead of router.navigate + return false; drop console.log noise #1168

Closed
opened 2026-10-02 04:44:19 +00:00 by spikerj · 1 comment
Owner

Why

projects/spikersoft/src/app/_guards/role.guard.ts denies access by calling router.navigate(["/"]) and then returning false (two places, ~L24 and ~L36). That starts a second navigation from inside a running one (race-prone, and the original navigation is reported as cancelled rather than redirected). The router's supported way is to return a RedirectCommand (or UrlTree); Angular 22.2 additionally lets guards throw a RedirectCommand.

The guard also console.logs the user's realm roles and the grant/deny result on every guarded navigation.

What changes for us

  • Return type → Promise<boolean | RedirectCommand>; replace both navigate + false with return new RedirectCommand(router.parseUrl("/")) (consider skipLocationChange/replaceUrl so Back doesn't bounce).
  • Remove the three console.log lines (or route through the logging service at debug level).
  • Update role.guard specs (assert the returned redirect instead of a navigate spy).

Acceptance criteria

  • no router.navigate inside the guard; specs cover unauthenticated, missing role, granted
  • no role data logged to the console

Size: S. Returning RedirectCommand works today; the throw variant needs Angular 22.2.

Links: Throw variant needs #1165.


Filed from the 2026-10-02 spikersoft-angular pnpm outdated changelog review.

## Why `projects/spikersoft/src/app/_guards/role.guard.ts` denies access by calling `router.navigate(["/"])` and then returning `false` (two places, ~L24 and ~L36). That starts a second navigation from inside a running one (race-prone, and the original navigation is reported as cancelled rather than redirected). The router's supported way is to **return a `RedirectCommand`** (or `UrlTree`); Angular **22.2** additionally lets guards **throw** a `RedirectCommand`. The guard also `console.log`s the user's realm roles and the grant/deny result on every guarded navigation. ## What changes for us - Return type → `Promise<boolean | RedirectCommand>`; replace both `navigate + false` with `return new RedirectCommand(router.parseUrl("/"))` (consider `skipLocationChange`/`replaceUrl` so Back doesn't bounce). - Remove the three `console.log` lines (or route through the logging service at debug level). - Update `role.guard` specs (assert the returned redirect instead of a `navigate` spy). ## Acceptance criteria - [ ] no `router.navigate` inside the guard; specs cover unauthenticated, missing role, granted - [ ] no role data logged to the console Size: **S**. Returning `RedirectCommand` works today; the *throw* variant needs Angular 22.2. **Links:** Throw variant needs #1165. --- _Filed from the 2026-10-02 spikersoft-angular `pnpm outdated` changelog review._
spikerj added the enhancement label 2026-10-02 04:44:19 +00:00
Author
Owner

Tracker sweep 2026-10-06. Closing: done by spikerj/spikersoft-angular#1417 (merged 2026-10-02, commit 3532af5a7).

Checked against angular master (projects/spikersoft/src/app/_guards/role.guard.ts):

  • Redirect, no second navigation: there is no router.navigate left in the guard. It returns Promise<boolean | UrlTree> and denies with return router.parseUrl("/"). A returned UrlTree is the router's supported redirect, and it gives the same behaviour as RedirectCommand without extra options. The same fix was applied to parent, reading-interest and staff-or-parent guards.
  • No role logging: the guard has no console.log calls left.
  • Specs: role.guard.spec.ts covers an open route, an unauthenticated user, granted (any of the roles), denied (none of the roles), a token without realm_access (fails closed), and the wait for the #1363 auth probe.

The optional throw variant still needs Angular 22.2 (#1165). Nothing requires it, so I'm not keeping this open for it.

Side note, outside this ticket: auth.guard.ts still has 6 console.log lines. They log the route URL and the auth/refresh state, but no role data.

**Tracker sweep 2026-10-06. Closing: done by spikerj/spikersoft-angular#1417 (merged 2026-10-02, commit 3532af5a7).** Checked against angular `master` (`projects/spikersoft/src/app/_guards/role.guard.ts`): - **Redirect, no second navigation:** there is no `router.navigate` left in the guard. It returns `Promise<boolean | UrlTree>` and denies with `return router.parseUrl("/")`. A returned `UrlTree` is the router's supported redirect, and it gives the same behaviour as `RedirectCommand` without extra options. The same fix was applied to `parent`, `reading-interest` and `staff-or-parent` guards. - **No role logging:** the guard has no `console.log` calls left. - **Specs:** `role.guard.spec.ts` covers an open route, an unauthenticated user, granted (any of the roles), denied (none of the roles), a token without `realm_access` (fails closed), and the wait for the #1363 auth probe. The optional *throw* variant still needs Angular 22.2 (#1165). Nothing requires it, so I'm not keeping this open for it. Side note, outside this ticket: `auth.guard.ts` still has 6 `console.log` lines. They log the route URL and the auth/refresh state, but no role data.
Sign in to join this conversation.