Skip to content

Commit

Permalink
Refactor app/guards
Browse files Browse the repository at this point in the history
  • Loading branch information
RandomTannenbaum committed Dec 20, 2024
1 parent cd4ab66 commit ed72e4b
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 8 deletions.
6 changes: 3 additions & 3 deletions frontend/src/app/guards/auth.guard.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const routerMock = {

const route = { queryParamMap: new Map() };

describe('authGuard', () => {
describe('AuthGuard', () => {
const executeGuard: CanActivateFn = (...guardParameters) =>
TestBed.runInInjectionContext(() => authGuard(...guardParameters));

Expand Down Expand Up @@ -48,7 +48,7 @@ describe('authGuard', () => {
expect(executeGuard).toBeTruthy();
});

it('should not call initCodeFlow if token is valid and call router if state param exist', async () => {
it('should not call initCodeFlow() if token is valid and call router if state param exist', async () => {
jest.spyOn(oAuthMock, 'hasValidIdToken').mockReturnValue(true);
route.queryParamMap.set('state', 1234);

Expand All @@ -72,7 +72,7 @@ describe('authGuard', () => {
expect(routerMock.navigateByUrl).not.toHaveBeenCalled();
});

it('should call initCodeFlow if token is invalid', async () => {
it('should call initCodeFlow() if token is invalid', async () => {
jest.spyOn(oAuthMock, 'hasValidIdToken').mockReturnValue(false);
const result = await runAuthGuardWithContext(authGuard);

Expand Down
10 changes: 5 additions & 5 deletions frontend/src/app/guards/auth.guard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@ import { inject } from '@angular/core';
import { OAuthService } from 'angular-oauth2-oidc';

export const authGuard: CanActivateFn = (route, state) => {
const oauthService = inject(OAuthService);
const oAuthService = inject(OAuthService);
const router = inject(Router);
return oauthService.loadDiscoveryDocumentAndTryLogin().then(async () => {
return oAuthService.loadDiscoveryDocumentAndTryLogin().then(async () => {
// if the login failed initialize code flow
let validToken = oauthService.hasValidIdToken();
let validToken = oAuthService.hasValidIdToken();
if (!validToken) {
oauthService.initCodeFlow();
oAuthService.initCodeFlow();
return false;
}
oauthService.setupAutomaticSilentRefresh();
oAuthService.setupAutomaticSilentRefresh();
// redirect route to remove state query param. do it only, if this param exist to avoid infinite loop
if (!!route.queryParamMap.get('state')) {
await router.navigateByUrl('');
Expand Down

0 comments on commit ed72e4b

Please sign in to comment.