Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add guard autoLoginPartialRoutesGuardWithConfig for specific configurations #2000

Merged
merged 2 commits into from
Sep 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading