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

deprecate guard and update docs #1782

Merged
merged 1 commit into from
Jun 19, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ const appRoutes: Routes = [

All other guard types like `canLoad` or `canActivateChild` work in a similar way. However, the guard class has to implement the respective interfaces and methods accordingly.

## Functional
## Functional API

```ts
import { inject } from '@angular/core';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ In case you are running multiple configurations all the configured routes over a

Keep in mind that you always can implement your own interceptor as [described in the Angular documentation](https://angular.io/guide/http#intercepting-requests-and-responses).

## Standalone API
## Functional API

To use the standalone API use the `authInterceptor` method
To use the functional API use the `authInterceptor` method

```ts
import { ApplicationConfig } from '@angular/core';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,7 @@ import { ConfigurationService } from '../config/config.service';
import { LoginResponse } from '../login/login-response';
import { LoginService } from '../login/login.service';
import { StoragePersistenceService } from '../storage/storage-persistence.service';
import {
AutoLoginAllRoutesGuard,
autoLoginAllRoutesGuard,
} from './auto-login-all-routes.guard';
import { AutoLoginAllRoutesGuard } from './auto-login-all-routes.guard';
import { AutoLoginService } from './auto-login.service';

describe(`AutoLoginAllRoutesGuard`, () => {
Expand Down Expand Up @@ -392,178 +389,4 @@ describe(`AutoLoginAllRoutesGuard`, () => {
}));
});
});

describe('functional', () => {
// describe('canActivate', () => {
// let guard: CanActivateFn;

// let checkAuthService: CheckAuthService;
// let loginService: LoginService;
// let storagePersistenceService: StoragePersistenceService;
// let configurationService: ConfigurationService;
// let autoLoginService: AutoLoginService;

// beforeEach(() => {
// checkAuthService = TestBed.inject(CheckAuthService);
// loginService = TestBed.inject(LoginService);
// storagePersistenceService = TestBed.inject(StoragePersistenceService);
// configurationService = TestBed.inject(ConfigurationService);

// spyOn(configurationService, 'getOpenIDConfiguration').and.returnValue(of({ configId: 'configId1' }));

// guard = TestBed.runInInjectionContext(autoLoginAllRoutesGuard);

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

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

// it('should save current route and call `login` if not authenticated already', waitForAsync(() => {
// spyOn(checkAuthService, 'checkAuth').and.returnValue(of({ isAuthenticated: false } as LoginResponse));
// const checkSavedRedirectRouteAndNavigateSpy = spyOn(autoLoginService, 'checkSavedRedirectRouteAndNavigate');
// const saveRedirectRouteSpy = spyOn(autoLoginService, 'saveRedirectRoute');
// const loginSpy = spyOn(loginService, 'login');

// const canActivate$ = guard(null, { url: 'some-url1' } as RouterStateSnapshot) as Observable<boolean>;

// canActivate$.subscribe(() => {
// expect(saveRedirectRouteSpy).toHaveBeenCalledOnceWith({ configId: 'configId1' }, 'some-url1');
// expect(loginSpy).toHaveBeenCalledOnceWith({ configId: 'configId1' });
// expect(checkSavedRedirectRouteAndNavigateSpy).not.toHaveBeenCalled();
// });
// }));

// it('should call `checkSavedRedirectRouteAndNavigate` if authenticated already', waitForAsync(() => {
// spyOn(checkAuthService, 'checkAuth').and.returnValue(of({ isAuthenticated: true } as LoginResponse));
// const checkSavedRedirectRouteAndNavigateSpy = spyOn(autoLoginService, 'checkSavedRedirectRouteAndNavigate');
// const saveRedirectRouteSpy = spyOn(autoLoginService, 'saveRedirectRoute');
// const loginSpy = spyOn(loginService, 'login');
// const canActivate$ = guard(null, { url: 'some-url1' } as RouterStateSnapshot) as Observable<boolean>;

// canActivate$.subscribe(() => {
// expect(saveRedirectRouteSpy).not.toHaveBeenCalled();
// expect(loginSpy).not.toHaveBeenCalled();
// expect(checkSavedRedirectRouteAndNavigateSpy).toHaveBeenCalledOnceWith({ configId: 'configId1' });
// });
// }));
// });

describe('autoLoginAllRoutesGuard', () => {
let checkAuthService: CheckAuthService;
let loginService: LoginService;
let storagePersistenceService: StoragePersistenceService;
let configurationService: ConfigurationService;
let autoLoginService: AutoLoginService;
let router: Router;

beforeEach(() => {
checkAuthService = TestBed.inject(CheckAuthService);
loginService = TestBed.inject(LoginService);

storagePersistenceService = TestBed.inject(StoragePersistenceService);
configurationService = TestBed.inject(ConfigurationService);

spyOn(configurationService, 'getOpenIDConfiguration').and.returnValue(
of({ configId: 'configId1' })
);

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

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

it('should save current route (empty) and call `login` if not authenticated already', waitForAsync(() => {
spyOn(checkAuthService, 'checkAuth').and.returnValue(
of({ isAuthenticated: false } as LoginResponse)
);
const checkSavedRedirectRouteAndNavigateSpy = spyOn(
autoLoginService,
'checkSavedRedirectRouteAndNavigate'
);
const saveRedirectRouteSpy = spyOn(
autoLoginService,
'saveRedirectRoute'
);
const loginSpy = spyOn(loginService, 'login');

const guard$ = TestBed.runInInjectionContext(autoLoginAllRoutesGuard);

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

it('should save current route (with router extractedUrl) and call `login` if not authenticated already', waitForAsync(() => {
spyOn(checkAuthService, 'checkAuth').and.returnValue(
of({ isAuthenticated: false } as LoginResponse)
);
const checkSavedRedirectRouteAndNavigateSpy = spyOn(
autoLoginService,
'checkSavedRedirectRouteAndNavigate'
);
const saveRedirectRouteSpy = spyOn(
autoLoginService,
'saveRedirectRoute'
);
const loginSpy = spyOn(loginService, 'login');
const _routerSpy = spyOn(
router,
'getCurrentNavigation'
).and.returnValue({
extractedUrl: router.parseUrl(
'some-url12/with/some-param?queryParam=true'
),
extras: {},
id: 1,
initialUrl: router.parseUrl(''),
previousNavigation: null,
trigger: 'imperative',
});
const guard$ = TestBed.runInInjectionContext(autoLoginAllRoutesGuard);

guard$.subscribe(() => {
expect(saveRedirectRouteSpy).toHaveBeenCalledOnceWith(
{ configId: 'configId1' },
'some-url12/with/some-param?queryParam=true'
);
expect(loginSpy).toHaveBeenCalledOnceWith({ configId: 'configId1' });
expect(checkSavedRedirectRouteAndNavigateSpy).not.toHaveBeenCalled();
});
}));

it('should call `checkSavedRedirectRouteAndNavigate` if authenticated already', waitForAsync(() => {
spyOn(checkAuthService, 'checkAuth').and.returnValue(
of({ isAuthenticated: true } as LoginResponse)
);
const checkSavedRedirectRouteAndNavigateSpy = spyOn(
autoLoginService,
'checkSavedRedirectRouteAndNavigate'
);
const saveRedirectRouteSpy = spyOn(
autoLoginService,
'saveRedirectRoute'
);
const loginSpy = spyOn(loginService, 'login');
const guard$ = TestBed.runInInjectionContext(autoLoginAllRoutesGuard);

guard$.subscribe(() => {
expect(saveRedirectRouteSpy).not.toHaveBeenCalled();
expect(loginSpy).not.toHaveBeenCalled();
expect(
checkSavedRedirectRouteAndNavigateSpy
).toHaveBeenCalledOnceWith({ configId: 'configId1' });
});
}));
});
});
});
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Injectable, inject } from '@angular/core';
import { Injectable } from '@angular/core';
import {
ActivatedRouteSnapshot,
Router,
Expand All @@ -12,6 +12,9 @@ import { ConfigurationService } from '../config/config.service';
import { LoginService } from '../login/login.service';
import { AutoLoginService } from './auto-login.service';

/**
* @deprecated Please do not use the `AutoLoginAllRoutesGuard` anymore as it is not recommended anymore, deprecated and will be removed in future versions of this library. More information [Why is AutoLoginAllRoutesGuard not recommended?](https://github.com/damienbod/angular-auth-oidc-client/issues/1549)
*/
@Injectable({ providedIn: 'root' })
export class AutoLoginAllRoutesGuard {
constructor(
Expand Down Expand Up @@ -65,34 +68,6 @@ export class AutoLoginAllRoutesGuard {
}
}

export function autoLoginAllRoutesGuard(): Observable<boolean> {
const configurationService = inject(ConfigurationService);
const checkAuthService = inject(CheckAuthService);
const loginService = inject(LoginService);
const autoLoginService = inject(AutoLoginService);
const router = inject(Router);

const url =
router.getCurrentNavigation()?.extractedUrl.toString().substring(1) ?? '';

return checkAuth(
url,
configurationService,
checkAuthService,
autoLoginService,
loginService
);
}

// export function autoLoginAllRoutesGuard(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> {
// const configurationService = inject(ConfigurationService);
// const checkAuthService = inject(CheckAuthService);
// const loginService = inject(LoginService);
// const autoLoginService = inject(AutoLoginService);

// return checkAuth(state.url, configurationService, checkAuthService, autoLoginService, loginService);
// }

function checkAuth(
url: string,
configurationService: ConfigurationService,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -345,75 +345,6 @@ describe(`AutoLoginPartialRoutesGuard`, () => {
});

describe('functional', () => {
// describe('canActivate', () => {
// 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.returnValue(of({ configId: 'configId1' }));

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

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

// it('should save current route 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 canActivate$: any = TestBed.runInInjectionContext(autoLoginPartialRoutesGuard as any);

// canActivate$.subscribe(() => {
// expect(saveRedirectRouteSpy).toHaveBeenCalledOnceWith({ configId: 'configId1' }, 'some-url1');
// expect(loginSpy).toHaveBeenCalledOnceWith({ configId: 'configId1' });
// expect(checkSavedRedirectRouteAndNavigateSpy).not.toHaveBeenCalled();
// });
// }));

// 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 canActivate$ = guard(
// { data: { custom: 'param' } } as unknown as ActivatedRouteSnapshot,
// { url: 'some-url1' } as RouterStateSnapshot
// ) as Observable<boolean>;

// canActivate$.subscribe(() => {
// expect(saveRedirectRouteSpy).toHaveBeenCalledOnceWith({ configId: 'configId1' }, 'some-url1');
// 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);
// const checkSavedRedirectRouteAndNavigateSpy = spyOn(autoLoginService, 'checkSavedRedirectRouteAndNavigate');
// const saveRedirectRouteSpy = spyOn(autoLoginService, 'saveRedirectRoute');
// const loginSpy = spyOn(loginService, 'login');
// const canActivate$ = guard(null, { url: 'some-url1' } as RouterStateSnapshot) as Observable<boolean>;

// canActivate$.subscribe(() => {
// expect(saveRedirectRouteSpy).not.toHaveBeenCalled();
// expect(loginSpy).not.toHaveBeenCalled();
// expect(checkSavedRedirectRouteAndNavigateSpy).toHaveBeenCalledOnceWith({ configId: 'configId1' });
// });
// }));
// });

describe('autoLoginPartialRoutesGuard', () => {
let loginService: LoginService;
let authStateService: AuthStateService;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,17 +94,6 @@ export function autoLoginPartialRoutesGuard(): Observable<boolean> {
);
}

// export function autoLoginPartialRoutesGuard(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> {
// const configurationService = inject(ConfigurationService);
// const authStateService = inject(AuthStateService);
// const loginService = inject(LoginService);
// const autoLoginService = inject(AutoLoginService);

// const authOptions: AuthOptions | undefined = route?.data ? { customParams: route.data } : undefined;

// return checkAuth(state.url, configurationService, authStateService, autoLoginService, loginService, authOptions);
// }

function checkAuth(
url: string,
configurationService: ConfigurationService,
Expand Down