Skip to content

Commit

Permalink
Merge pull request #2000 from kyubisation/feat-guard-config
Browse files Browse the repository at this point in the history
feat: add guard `autoLoginPartialRoutesGuardWithConfig` for specific configurations
  • Loading branch information
FabianGosebrink authored Sep 18, 2024
2 parents d23eccb + 51d4fb7 commit 20ac4eb
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 10 deletions.
12 changes: 6 additions & 6 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ jobs:
run: ls

- name: Upload Artefact
uses: actions/upload-artifact@v2
uses: actions/upload-artifact@v3
with:
name: angular_auth_oidc_client_artefact
path: dist/angular-auth-oidc-client
Expand All @@ -71,7 +71,7 @@ jobs:
node-version: 20

- name: Download Artefact
uses: actions/download-artifact@v2
uses: actions/download-artifact@v3
with:
name: angular_auth_oidc_client_artefact
path: angular-auth-oidc-client-artefact
Expand Down Expand Up @@ -111,7 +111,7 @@ jobs:
node-version: 20

- name: Download Artefact
uses: actions/download-artifact@v2
uses: actions/download-artifact@v3
with:
name: angular_auth_oidc_client_artefact
path: angular-auth-oidc-client-artefact
Expand Down Expand Up @@ -151,7 +151,7 @@ jobs:
node-version: 20

- name: Download Artefact
uses: actions/download-artifact@v2
uses: actions/download-artifact@v3
with:
name: angular_auth_oidc_client_artefact
path: angular-auth-oidc-client-artefact
Expand Down Expand Up @@ -191,7 +191,7 @@ jobs:
node-version: 20

- name: Download Artefact
uses: actions/download-artifact@v2
uses: actions/download-artifact@v3
with:
name: angular_auth_oidc_client_artefact
path: angular-auth-oidc-client-artefact
Expand Down Expand Up @@ -235,7 +235,7 @@ jobs:
node-version: 18

- name: Download Artefact
uses: actions/download-artifact@v2
uses: actions/download-artifact@v3
with:
name: angular_auth_oidc_client_artefact
path: angular-auth-oidc-client-artefact
Expand Down
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 20ac4eb

Please sign in to comment.