Skip to content

Commit

Permalink
feat: add guard autoLoginPartialRoutesGuardWithConfig for specific …
Browse files Browse the repository at this point in the history
…configurations
  • Loading branch information
kyubisation committed Sep 17, 2024
1 parent d23eccb commit decfb95
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { StoragePersistenceService } from '../storage/storage-persistence.servic
import {
AutoLoginPartialRoutesGuard,
autoLoginPartialRoutesGuard,
autoLoginPartialRoutesGuardWithConfig,
} from './auto-login-partial-routes.guard';
import { AutoLoginService } from './auto-login.service';

Expand Down Expand Up @@ -498,5 +499,58 @@ describe(`AutoLoginPartialRoutesGuard`, () => {
});
}));
});

describe('autoLoginPartialRoutesGuardWithConfig', () => {
let loginService: LoginService;
let authStateService: AuthStateService;
let storagePersistenceService: StoragePersistenceService;
let configurationService: ConfigurationService;
let autoLoginService: AutoLoginService;

beforeEach(() => {
authStateService = TestBed.inject(AuthStateService);
loginService = TestBed.inject(LoginService);
storagePersistenceService = TestBed.inject(StoragePersistenceService);
configurationService = TestBed.inject(ConfigurationService);

spyOn(configurationService, 'getOpenIDConfiguration').and.callFake(
(configId) => of({ configId })
);

autoLoginService = TestBed.inject(AutoLoginService);
});

afterEach(() => {
storagePersistenceService.clear({});
});

it('should save current route (empty) and call `login` if not authenticated already', 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(
autoLoginPartialRoutesGuardWithConfig('configId1')
);

guard$.subscribe(() => {
expect(saveRedirectRouteSpy).toHaveBeenCalledOnceWith(
{ configId: 'configId1' },
''
);
expect(loginSpy).toHaveBeenCalledOnceWith({ configId: 'configId1' });
expect(checkSavedRedirectRouteAndNavigateSpy).not.toHaveBeenCalled();
});
}));
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@ export class AutoLoginPartialRoutesGuard {
}

export function autoLoginPartialRoutesGuard(
route?: ActivatedRouteSnapshot
route?: ActivatedRouteSnapshot,
configId?: string
): Observable<boolean> {
const configurationService = inject(ConfigurationService);
const authStateService = inject(AuthStateService);
Expand All @@ -98,19 +99,28 @@ export function autoLoginPartialRoutesGuard(
authStateService,
autoLoginService,
loginService,
authOptions
authOptions,
configId
);
}

export function autoLoginPartialRoutesGuardWithConfig(
configId: string
): (route?: ActivatedRouteSnapshot) => Observable<boolean> {
return (route?: ActivatedRouteSnapshot) =>
autoLoginPartialRoutesGuard(route, configId);
}

function checkAuth(
url: string,
configurationService: ConfigurationService,
authStateService: AuthStateService,
autoLoginService: AutoLoginService,
loginService: LoginService,
authOptions?: AuthOptions
authOptions?: AuthOptions,
configId?: string
): Observable<boolean> {
return configurationService.getOpenIDConfiguration().pipe(
return configurationService.getOpenIDConfiguration(configId).pipe(
map((configuration) => {
const isAuthenticated =
authStateService.areAuthStorageTokensValid(configuration);
Expand Down

0 comments on commit decfb95

Please sign in to comment.