Skip to content

Commit ae79092

Browse files
Merge pull request #1964 from timdeschryver/update-functional-guard
feat: add support for route data to autoLoginPartialRoutesGuard
2 parents 6f3fa8e + d57ffb7 commit ae79092

File tree

2 files changed

+37
-2
lines changed

2 files changed

+37
-2
lines changed

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

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,37 @@ describe(`AutoLoginPartialRoutesGuard`, () => {
438438
});
439439
}));
440440

441+
it('should save current route and call `login` if not authenticated already and add custom params', waitForAsync(() => {
442+
spyOn(authStateService, 'areAuthStorageTokensValid').and.returnValue(
443+
false
444+
);
445+
const checkSavedRedirectRouteAndNavigateSpy = spyOn(
446+
autoLoginService,
447+
'checkSavedRedirectRouteAndNavigate'
448+
);
449+
const saveRedirectRouteSpy = spyOn(
450+
autoLoginService,
451+
'saveRedirectRoute'
452+
);
453+
const loginSpy = spyOn(loginService, 'login');
454+
455+
const guard$ = TestBed.runInInjectionContext(
456+
() => autoLoginPartialRoutesGuard({data: {custom: 'param'}} as unknown as ActivatedRouteSnapshot)
457+
);
458+
459+
guard$.subscribe(() => {
460+
expect(saveRedirectRouteSpy).toHaveBeenCalledOnceWith(
461+
{ configId: 'configId1' },
462+
''
463+
);
464+
expect(loginSpy).toHaveBeenCalledOnceWith(
465+
{ configId: 'configId1' },
466+
{ customParams: { custom: 'param' } }
467+
);
468+
expect(checkSavedRedirectRouteAndNavigateSpy).not.toHaveBeenCalled();
469+
});
470+
}));
471+
441472
it('should call `checkSavedRedirectRouteAndNavigate` if authenticated already', waitForAsync(() => {
442473
spyOn(authStateService, 'areAuthStorageTokensValid').and.returnValue(
443474
true

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,12 +77,15 @@ export class AutoLoginPartialRoutesGuard {
7777
}
7878
}
7979

80-
export function autoLoginPartialRoutesGuard(): Observable<boolean> {
80+
export function autoLoginPartialRoutesGuard(route?: ActivatedRouteSnapshot): Observable<boolean> {
8181
const configurationService = inject(ConfigurationService);
8282
const authStateService = inject(AuthStateService);
8383
const loginService = inject(LoginService);
8484
const autoLoginService = inject(AutoLoginService);
8585
const router = inject(Router);
86+
const authOptions: AuthOptions | undefined = route?.data
87+
? { customParams: route.data }
88+
: undefined;
8689

8790
const url =
8891
router.getCurrentNavigation()?.extractedUrl.toString().substring(1) ?? '';
@@ -92,7 +95,8 @@ export function autoLoginPartialRoutesGuard(): Observable<boolean> {
9295
configurationService,
9396
authStateService,
9497
autoLoginService,
95-
loginService
98+
loginService,
99+
authOptions
96100
);
97101
}
98102

0 commit comments

Comments
 (0)