Skip to content

Commit

Permalink
Merge pull request #1964 from timdeschryver/update-functional-guard
Browse files Browse the repository at this point in the history
feat: add support for route data to autoLoginPartialRoutesGuard
  • Loading branch information
FabianGosebrink authored Jun 13, 2024
2 parents 6f3fa8e + d57ffb7 commit ae79092
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,37 @@ describe(`AutoLoginPartialRoutesGuard`, () => {
});
}));

it('should save current route and call `login` if not authenticated already and add custom params', waitForAsync(() => {
spyOn(authStateService, 'areAuthStorageTokensValid').and.returnValue(
false
);
const checkSavedRedirectRouteAndNavigateSpy = spyOn(
autoLoginService,
'checkSavedRedirectRouteAndNavigate'
);
const saveRedirectRouteSpy = spyOn(
autoLoginService,
'saveRedirectRoute'
);
const loginSpy = spyOn(loginService, 'login');

const guard$ = TestBed.runInInjectionContext(
() => autoLoginPartialRoutesGuard({data: {custom: 'param'}} as unknown as ActivatedRouteSnapshot)
);

guard$.subscribe(() => {
expect(saveRedirectRouteSpy).toHaveBeenCalledOnceWith(
{ configId: 'configId1' },
''
);
expect(loginSpy).toHaveBeenCalledOnceWith(
{ configId: 'configId1' },
{ customParams: { custom: 'param' } }
);
expect(checkSavedRedirectRouteAndNavigateSpy).not.toHaveBeenCalled();
});
}));

it('should call `checkSavedRedirectRouteAndNavigate` if authenticated already', waitForAsync(() => {
spyOn(authStateService, 'areAuthStorageTokensValid').and.returnValue(
true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,15 @@ export class AutoLoginPartialRoutesGuard {
}
}

export function autoLoginPartialRoutesGuard(): Observable<boolean> {
export function autoLoginPartialRoutesGuard(route?: ActivatedRouteSnapshot): Observable<boolean> {
const configurationService = inject(ConfigurationService);
const authStateService = inject(AuthStateService);
const loginService = inject(LoginService);
const autoLoginService = inject(AutoLoginService);
const router = inject(Router);
const authOptions: AuthOptions | undefined = route?.data
? { customParams: route.data }
: undefined;

const url =
router.getCurrentNavigation()?.extractedUrl.toString().substring(1) ?? '';
Expand All @@ -92,7 +95,8 @@ export function autoLoginPartialRoutesGuard(): Observable<boolean> {
configurationService,
authStateService,
autoLoginService,
loginService
loginService,
authOptions
);
}

Expand Down

0 comments on commit ae79092

Please sign in to comment.