From 3dafaff2c01236ae7065a299f1bde62f869a89e9 Mon Sep 17 00:00:00 2001 From: gbrodman Date: Tue, 12 Sep 2023 15:37:12 -0400 Subject: [PATCH] Pass around the full URL in RegistrarGuard (#2139) Previously this didn't properly deal with nested routings, e.g. "settings/whois". It tried to just pass "whois" as the next url which doesn't work with the router because it's nested under the settings. Using all parts of the URL allows us to handle the nesting. --- console-webapp/src/app/registrar/registrar.guard.spec.ts | 6 +++--- console-webapp/src/app/registrar/registrar.guard.ts | 9 ++++++--- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/console-webapp/src/app/registrar/registrar.guard.spec.ts b/console-webapp/src/app/registrar/registrar.guard.spec.ts index 0e668b046..c9157ec4f 100644 --- a/console-webapp/src/app/registrar/registrar.guard.spec.ts +++ b/console-webapp/src/app/registrar/registrar.guard.spec.ts @@ -39,7 +39,7 @@ describe('RegistrarGuard', () => { it('should not be able to activate when activeRegistrarId is empty', () => { guard = TestBed.inject(RegistrarGuard); - const res = guard.canActivate(dummyRoute); + const res = guard.canActivate(); expect(res).toBeFalsy(); }); @@ -48,14 +48,14 @@ describe('RegistrarGuard', () => { useValue: { activeRegistrarId: 'value' }, }); guard = TestBed.inject(RegistrarGuard); - const res = guard.canActivate(dummyRoute); + const res = guard.canActivate(); expect(res).toBeTrue(); }); it('should navigate to registrars when activeRegistrarId is empty', () => { const dummyRoute = { url: '/value' } as RouterStateSnapshot; guard = TestBed.inject(RegistrarGuard); - guard.canActivate(dummyRoute); + guard.canActivate(); expect(routeSpy.navigate).toHaveBeenCalledOnceWith([ '/registrars', { nextUrl: '/value' }, diff --git a/console-webapp/src/app/registrar/registrar.guard.ts b/console-webapp/src/app/registrar/registrar.guard.ts index 5ca5719d7..f0dd9869a 100644 --- a/console-webapp/src/app/registrar/registrar.guard.ts +++ b/console-webapp/src/app/registrar/registrar.guard.ts @@ -13,7 +13,8 @@ // limitations under the License. import { Injectable } from '@angular/core'; -import { Router, RouterStateSnapshot } from '@angular/router'; +import { Router } from '@angular/router'; + import { RegistrarService } from './registrar.service'; @Injectable({ @@ -25,10 +26,12 @@ export class RegistrarGuard { private registrarService: RegistrarService ) {} - canActivate(state: RouterStateSnapshot): Promise | boolean { + canActivate(): Promise | boolean { if (this.registrarService.activeRegistrarId) { return true; } - return this.router.navigate([`/empty-registrar`, { nextUrl: state.url }]); + // Get the full URL including any nested children (skip the initial '#/') + const nextUrl = location.hash.split('#/')[1]; + return this.router.navigate([`/empty-registrar`, { nextUrl }]); } }