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.
This commit is contained in:
gbrodman 2023-09-12 15:37:12 -04:00 committed by GitHub
parent ca25e4dfbd
commit 3dafaff2c0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 9 additions and 6 deletions

View file

@ -39,7 +39,7 @@ describe('RegistrarGuard', () => {
it('should not be able to activate when activeRegistrarId is empty', () => { it('should not be able to activate when activeRegistrarId is empty', () => {
guard = TestBed.inject(RegistrarGuard); guard = TestBed.inject(RegistrarGuard);
const res = guard.canActivate(dummyRoute); const res = guard.canActivate();
expect(res).toBeFalsy(); expect(res).toBeFalsy();
}); });
@ -48,14 +48,14 @@ describe('RegistrarGuard', () => {
useValue: { activeRegistrarId: 'value' }, useValue: { activeRegistrarId: 'value' },
}); });
guard = TestBed.inject(RegistrarGuard); guard = TestBed.inject(RegistrarGuard);
const res = guard.canActivate(dummyRoute); const res = guard.canActivate();
expect(res).toBeTrue(); expect(res).toBeTrue();
}); });
it('should navigate to registrars when activeRegistrarId is empty', () => { it('should navigate to registrars when activeRegistrarId is empty', () => {
const dummyRoute = { url: '/value' } as RouterStateSnapshot; const dummyRoute = { url: '/value' } as RouterStateSnapshot;
guard = TestBed.inject(RegistrarGuard); guard = TestBed.inject(RegistrarGuard);
guard.canActivate(dummyRoute); guard.canActivate();
expect(routeSpy.navigate).toHaveBeenCalledOnceWith([ expect(routeSpy.navigate).toHaveBeenCalledOnceWith([
'/registrars', '/registrars',
{ nextUrl: '/value' }, { nextUrl: '/value' },

View file

@ -13,7 +13,8 @@
// limitations under the License. // limitations under the License.
import { Injectable } from '@angular/core'; import { Injectable } from '@angular/core';
import { Router, RouterStateSnapshot } from '@angular/router'; import { Router } from '@angular/router';
import { RegistrarService } from './registrar.service'; import { RegistrarService } from './registrar.service';
@Injectable({ @Injectable({
@ -25,10 +26,12 @@ export class RegistrarGuard {
private registrarService: RegistrarService private registrarService: RegistrarService
) {} ) {}
canActivate(state: RouterStateSnapshot): Promise<boolean> | boolean { canActivate(): Promise<boolean> | boolean {
if (this.registrarService.activeRegistrarId) { if (this.registrarService.activeRegistrarId) {
return true; 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 }]);
} }
} }