Merge branch 'w2p-72699_Hard-redirect-after-log-in' into w2p-72541_User-agreement-and-Privacy-statement

This commit is contained in:
Kristof De Langhe
2020-08-28 11:46:38 +02:00
2 changed files with 8 additions and 42 deletions

View File

@@ -322,31 +322,28 @@ describe('AuthService test', () => {
expect(storage.remove).toHaveBeenCalled();
});
it('should set redirect url to previous page', () => {
(storage.get as jasmine.Spy).and.returnValue('/collection/123');
authService.redirectAfterLoginSuccess();
it('should redirect to reload with redirect url', () => {
authService.navigateToRedirectUrl('/collection/123');
// Reload with redirect URL set to /collection/123
expect(hardRedirectService.redirect).toHaveBeenCalledWith(jasmine.stringMatching(new RegExp('/reload/[0-9]*\\?redirect=' + encodeURIComponent('/collection/123'))));
});
it('should set redirect url to current page', () => {
(storage.get as jasmine.Spy).and.returnValue('/home');
authService.redirectAfterLoginSuccess();
it('should redirect to reload with /home', () => {
authService.navigateToRedirectUrl('/home');
// Reload with redirect URL set to /home
expect(hardRedirectService.redirect).toHaveBeenCalledWith(jasmine.stringMatching(new RegExp('/reload/[0-9]*\\?redirect=' + encodeURIComponent('/home'))));
});
it('should redirect to regular reload and not to /login', () => {
(storage.get as jasmine.Spy).and.returnValue('/login');
authService.redirectAfterLoginSuccess();
authService.navigateToRedirectUrl('/login');
// Reload without a redirect URL
expect(hardRedirectService.redirect).toHaveBeenCalledWith(jasmine.stringMatching(new RegExp('/reload/[0-9]*(?!\\?)$')));
});
it('should not redirect when no redirect url is found', () => {
authService.redirectAfterLoginSuccess();
it('should redirect to regular reload when no redirect url is found', () => {
authService.navigateToRedirectUrl(undefined);
// Reload without a redirect URL
expect(hardRedirectService.redirect).not.toHaveBeenCalled();
expect(hardRedirectService.redirect).toHaveBeenCalledWith(jasmine.stringMatching(new RegExp('/reload/[0-9]*(?!\\?)$')));
});
describe('impersonate', () => {

View File

@@ -78,37 +78,6 @@ export class AuthService {
select(isAuthenticated),
startWith(false)
).subscribe((authenticated: boolean) => this._authenticated = authenticated);
// If current route is different from the one setted in authentication guard
// and is not the login route, clear redirect url and messages
const routeUrl$ = this.store.pipe(
select(routerStateSelector),
filter((routerState: RouterReducerState) => isNotUndefined(routerState)
&& isNotUndefined(routerState.state) && isNotEmpty(routerState.state.url)),
filter((routerState: RouterReducerState) => !this.isLoginRoute(routerState.state.url)),
map((routerState: RouterReducerState) => routerState.state.url)
);
const redirectUrl$ = this.store.pipe(select(getRedirectUrl), distinctUntilChanged());
routeUrl$.pipe(
withLatestFrom(redirectUrl$),
map(([routeUrl, redirectUrl]) => [routeUrl, redirectUrl])
).pipe(filter(([routeUrl, redirectUrl]) => isNotEmpty(redirectUrl) && (routeUrl !== redirectUrl)))
.subscribe(() => {
this.clearRedirectUrl();
});
}
/**
* Check if is a login page route
*
* @param {string} url
* @returns {Boolean}.
*/
protected isLoginRoute(url: string) {
const urlTree: UrlTree = this.router.parseUrl(url);
const g: UrlSegmentGroup = urlTree.root.children[PRIMARY_OUTLET];
const segment = '/' + g.toString();
return segment === LOGIN_ROUTE;
}
/**