Skip to content

Commit bc93390

Browse files
authored
Merge pull request #1051 from TomaszKasowicz/TomaszKasowicz/use-navigateByUrl
fix: use navigateByUrl to fix url params encoding
2 parents 2bcabd6 + 39245b3 commit bc93390

10 files changed

+26
-26
lines changed

docs/guards.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export class AuthorizationGuard implements CanActivate {
1919
console.log('AuthorizationGuard, canActivate isAuthorized: ' + isAuthorized);
2020

2121
if (!isAuthorized) {
22-
this.router.navigate(['/unauthorized']);
22+
this.router.navigateByUrl('/unauthorized');
2323
return false;
2424
}
2525

projects/angular-auth-oidc-client/src/lib/auto-login/auto-login.guard.spec.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -130,13 +130,13 @@ describe(`AutoLoginGuard`, () => {
130130
spyOn(checkAuthService, 'checkAuth').and.returnValue(of(true));
131131
spyOn(localStorage, 'getItem').and.returnValue('stored-route');
132132
const localStorageSpy = spyOn(localStorage, 'removeItem');
133-
const routerSpy = spyOn(router, 'navigate');
133+
const routerSpy = spyOn(router, 'navigateByUrl');
134134
const loginSpy = spyOn(loginService, 'login');
135135

136136
autoLoginGuard.canActivate(null, { url: 'some-url7' } as RouterStateSnapshot).subscribe((result) => {
137137
expect(result).toBe(true);
138138
expect(localStorageSpy).toHaveBeenCalledOnceWith('redirect');
139-
expect(routerSpy).toHaveBeenCalledOnceWith(['stored-route']);
139+
expect(routerSpy).toHaveBeenCalledOnceWith('stored-route');
140140
expect(loginSpy).not.toHaveBeenCalled();
141141
});
142142
})
@@ -221,13 +221,13 @@ describe(`AutoLoginGuard`, () => {
221221
spyOn(checkAuthService, 'checkAuth').and.returnValue(of(true));
222222
spyOn(localStorage, 'getItem').and.returnValue('stored-route');
223223
const localStorageSpy = spyOn(localStorage, 'removeItem');
224-
const routerSpy = spyOn(router, 'navigate');
224+
const routerSpy = spyOn(router, 'navigateByUrl');
225225
const loginSpy = spyOn(loginService, 'login');
226226

227227
autoLoginGuard.canLoad({ path: 'some-url14' }, []).subscribe((result) => {
228228
expect(result).toBe(true);
229229
expect(localStorageSpy).toHaveBeenCalledOnceWith('redirect');
230-
expect(routerSpy).toHaveBeenCalledOnceWith(['stored-route']);
230+
expect(routerSpy).toHaveBeenCalledOnceWith('stored-route');
231231
expect(loginSpy).not.toHaveBeenCalled();
232232
});
233233
})

projects/angular-auth-oidc-client/src/lib/auto-login/auto-login.guard.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ export class AutoLoginGuard implements CanActivate, CanLoad {
3535
if (isAuthorized) {
3636
if (storedRoute) {
3737
this.autoLoginService.deleteStoredRedirectRoute();
38-
this.router.navigate([storedRoute]);
38+
this.router.navigateByUrl(storedRoute);
3939
}
4040
return true;
4141
}

projects/angular-auth-oidc-client/src/lib/callback/code-flow-callback.service.spec.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ describe('CodeFlowCallbackService ', () => {
6969
existingIdToken: '',
7070
};
7171
const spy = spyOn(flowsService, 'processCodeFlowCallback').and.returnValue(of(callbackContext));
72-
const routerSpy = spyOn(router, 'navigate');
72+
const routerSpy = spyOn(router, 'navigateByUrl');
7373
spyOn(configurationProvider, 'getOpenIDConfiguration').and.returnValue({ triggerAuthorizationResultEvent: true });
7474
codeFlowCallbackService.authorizedCallbackWithCode('some-url2').subscribe(() => {
7575
expect(spy).toHaveBeenCalledWith('some-url2');
@@ -93,14 +93,14 @@ describe('CodeFlowCallbackService ', () => {
9393
existingIdToken: '',
9494
};
9595
const spy = spyOn(flowsService, 'processCodeFlowCallback').and.returnValue(of(callbackContext));
96-
const routerSpy = spyOn(router, 'navigate');
96+
const routerSpy = spyOn(router, 'navigateByUrl');
9797
spyOn(configurationProvider, 'getOpenIDConfiguration').and.returnValue({
9898
triggerAuthorizationResultEvent: false,
9999
postLoginRoute: 'postLoginRoute',
100100
});
101101
codeFlowCallbackService.authorizedCallbackWithCode('some-url3').subscribe(() => {
102102
expect(spy).toHaveBeenCalledWith('some-url3');
103-
expect(routerSpy).toHaveBeenCalledWith(['postLoginRoute']);
103+
expect(routerSpy).toHaveBeenCalledWith('postLoginRoute');
104104
});
105105
})
106106
);
@@ -134,7 +134,7 @@ describe('CodeFlowCallbackService ', () => {
134134
spyOn(flowsService, 'processCodeFlowCallback').and.returnValue(throwError('error'));
135135
const resetSilentRenewRunningSpy = spyOn(flowsDataService, 'resetSilentRenewRunning');
136136
const stopPeriodicallTokenCheckSpy = spyOn(intervallService, 'stopPeriodicallTokenCheck');
137-
const routerSpy = spyOn(router, 'navigate');
137+
const routerSpy = spyOn(router, 'navigateByUrl');
138138

139139
spyOn(configurationProvider, 'getOpenIDConfiguration').and.returnValue({
140140
triggerAuthorizationResultEvent: false,
@@ -145,7 +145,7 @@ describe('CodeFlowCallbackService ', () => {
145145
expect(resetSilentRenewRunningSpy).toHaveBeenCalled();
146146
expect(stopPeriodicallTokenCheckSpy).toHaveBeenCalled();
147147
expect(err).toBeTruthy();
148-
expect(routerSpy).toHaveBeenCalledWith(['unauthorizedRoute']);
148+
expect(routerSpy).toHaveBeenCalledWith('unauthorizedRoute');
149149
},
150150
});
151151
})

projects/angular-auth-oidc-client/src/lib/callback/code-flow-callback.service.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,14 @@ export class CodeFlowCallbackService {
2424
return this.flowsService.processCodeFlowCallback(urlToCheck).pipe(
2525
tap((callbackContext) => {
2626
if (!triggerAuthorizationResultEvent && !callbackContext.isRenewProcess) {
27-
this.router.navigate([postLoginRoute]);
27+
this.router.navigateByUrl(postLoginRoute);
2828
}
2929
}),
3030
catchError((error) => {
3131
this.flowsDataService.resetSilentRenewRunning();
3232
this.intervallService.stopPeriodicallTokenCheck();
3333
if (!triggerAuthorizationResultEvent && !isRenewProcess) {
34-
this.router.navigate([unauthorizedRoute]);
34+
this.router.navigateByUrl(unauthorizedRoute);
3535
}
3636
return throwError(error);
3737
})

projects/angular-auth-oidc-client/src/lib/callback/implicit-flow-callback.service.spec.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ describe('ImplicitFlowCallbackService ', () => {
7070
existingIdToken: '',
7171
};
7272
const spy = spyOn(flowsService, 'processImplicitFlowCallback').and.returnValue(of(callbackContext));
73-
const routerSpy = spyOn(router, 'navigate');
73+
const routerSpy = spyOn(router, 'navigateByUrl');
7474
spyOn(configurationProvider, 'getOpenIDConfiguration').and.returnValue({ triggerAuthorizationResultEvent: true });
7575
implicitFlowCallbackService.authorizedImplicitFlowCallback('some-hash').subscribe(() => {
7676
expect(spy).toHaveBeenCalledWith('some-hash');
@@ -94,14 +94,14 @@ describe('ImplicitFlowCallbackService ', () => {
9494
existingIdToken: '',
9595
};
9696
const spy = spyOn(flowsService, 'processImplicitFlowCallback').and.returnValue(of(callbackContext));
97-
const routerSpy = spyOn(router, 'navigate');
97+
const routerSpy = spyOn(router, 'navigateByUrl');
9898
spyOn(configurationProvider, 'getOpenIDConfiguration').and.returnValue({
9999
triggerAuthorizationResultEvent: false,
100100
postLoginRoute: 'postLoginRoute',
101101
});
102102
implicitFlowCallbackService.authorizedImplicitFlowCallback('some-hash').subscribe(() => {
103103
expect(spy).toHaveBeenCalledWith('some-hash');
104-
expect(routerSpy).toHaveBeenCalledWith(['postLoginRoute']);
104+
expect(routerSpy).toHaveBeenCalledWith('postLoginRoute');
105105
});
106106
})
107107
);
@@ -135,7 +135,7 @@ describe('ImplicitFlowCallbackService ', () => {
135135
spyOn(flowsService, 'processImplicitFlowCallback').and.returnValue(throwError('error'));
136136
const resetSilentRenewRunningSpy = spyOn(flowsDataService, 'resetSilentRenewRunning');
137137
const stopPeriodicallTokenCheckSpy = spyOn(intervalService, 'stopPeriodicallTokenCheck');
138-
const routerSpy = spyOn(router, 'navigate');
138+
const routerSpy = spyOn(router, 'navigateByUrl');
139139

140140
spyOn(configurationProvider, 'getOpenIDConfiguration').and.returnValue({
141141
triggerAuthorizationResultEvent: false,
@@ -146,7 +146,7 @@ describe('ImplicitFlowCallbackService ', () => {
146146
expect(resetSilentRenewRunningSpy).toHaveBeenCalled();
147147
expect(stopPeriodicallTokenCheckSpy).toHaveBeenCalled();
148148
expect(err).toBeTruthy();
149-
expect(routerSpy).toHaveBeenCalledWith(['unauthorizedRoute']);
149+
expect(routerSpy).toHaveBeenCalledWith('unauthorizedRoute');
150150
},
151151
});
152152
})

projects/angular-auth-oidc-client/src/lib/callback/implicit-flow-callback.service.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,14 @@ export class ImplicitFlowCallbackService {
2424
return this.flowsService.processImplicitFlowCallback(hash).pipe(
2525
tap((callbackContext) => {
2626
if (!triggerAuthorizationResultEvent && !callbackContext.isRenewProcess) {
27-
this.router.navigate([postLoginRoute]);
27+
this.router.navigateByUrl(postLoginRoute);
2828
}
2929
}),
3030
catchError((error) => {
3131
this.flowsDataService.resetSilentRenewRunning();
3232
this.intervalService.stopPeriodicallTokenCheck();
3333
if (!triggerAuthorizationResultEvent && !isRenewProcess) {
34-
this.router.navigate([unauthorizedRoute]);
34+
this.router.navigateByUrl(unauthorizedRoute);
3535
}
3636
return throwError(error);
3737
})

projects/angular-auth-oidc-client/src/lib/check-auth.service.spec.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -296,11 +296,11 @@ describe('CheckAuthService', () => {
296296
spyOn(authStateService, 'areAuthStorageTokensValid').and.returnValue(true);
297297
spyOn(autoLoginService, 'getStoredRedirectRoute').and.returnValue('some-saved-route');
298298
const deleteSpy = spyOn(autoLoginService, 'deleteStoredRedirectRoute');
299-
const routeSpy = spyOn(router, 'navigate');
299+
const routeSpy = spyOn(router, 'navigateByUrl');
300300

301301
checkAuthService.checkAuth().subscribe((result) => {
302302
expect(deleteSpy).toHaveBeenCalledTimes(1);
303-
expect(routeSpy).toHaveBeenCalledOnceWith(['some-saved-route']);
303+
expect(routeSpy).toHaveBeenCalledOnceWith('some-saved-route');
304304
});
305305
})
306306
);

projects/angular-auth-oidc-client/src/lib/check-auth.service.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ export class CheckAuthService {
7676
const savedRouteForRedirect = this.autoLoginService.getStoredRedirectRoute();
7777
if (savedRouteForRedirect) {
7878
this.autoLoginService.deleteStoredRedirectRoute();
79-
this.router.navigate([savedRouteForRedirect]);
79+
this.router.navigateByUrl(savedRouteForRedirect);
8080
}
8181
}),
8282
catchError((error) => {

projects/sample-implicit-flow-google/src/app/app.component.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export class AppComponent implements OnInit, OnDestroy {
1717
if (!isAuthenticated) {
1818
if ('/autologin' !== window.location.pathname) {
1919
this.write('redirect', window.location.pathname);
20-
this.router.navigate(['/autologin']);
20+
this.router.navigateByUrl('/autologin');
2121
}
2222
}
2323
if (isAuthenticated) {
@@ -51,9 +51,9 @@ export class AppComponent implements OnInit, OnDestroy {
5151
}
5252

5353
if (path.toString().includes('/unauthorized')) {
54-
this.router.navigate(['/']);
54+
this.router.navigateByUrl('/');
5555
} else {
56-
this.router.navigate([path]);
56+
this.router.navigateByUrl(path);
5757
}
5858
}
5959

0 commit comments

Comments
 (0)