Skip to content

Commit d4fb203

Browse files
[DURACOM-380] backporting
1 parent b325074 commit d4fb203

File tree

3 files changed

+90
-2
lines changed

3 files changed

+90
-2
lines changed

src/app/app-routes.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import { COLLECTION_MODULE_PATH } from './collection-page/collection-page-routin
2525
import { COMMUNITY_MODULE_PATH } from './community-page/community-page-routing-paths';
2626
import { authBlockingGuard } from './core/auth/auth-blocking.guard';
2727
import { authenticatedGuard } from './core/auth/authenticated.guard';
28+
import { notAuthenticatedGuard } from './core/auth/not-authenticated.guard';
2829
import { groupAdministratorGuard } from './core/data/feature-authorization/feature-authorization-guard/group-administrator.guard';
2930
import { siteAdministratorGuard } from './core/data/feature-authorization/feature-authorization-guard/site-administrator.guard';
3031
import { siteRegisterGuard } from './core/data/feature-authorization/feature-authorization-guard/site-register.guard';
@@ -97,13 +98,13 @@ export const APP_ROUTES: Route[] = [
9798
path: REGISTER_PATH,
9899
loadChildren: () => import('./register-page/register-page-routes')
99100
.then((m) => m.ROUTES),
100-
canActivate: [siteRegisterGuard],
101+
canActivate: [notAuthenticatedGuard, siteRegisterGuard],
101102
},
102103
{
103104
path: FORGOT_PASSWORD_PATH,
104105
loadChildren: () => import('./forgot-password/forgot-password-routes')
105106
.then((m) => m.ROUTES),
106-
canActivate: [endUserAgreementCurrentUserGuard, forgotPasswordCheckGuard],
107+
canActivate: [notAuthenticatedGuard, endUserAgreementCurrentUserGuard, forgotPasswordCheckGuard],
107108
},
108109
{
109110
path: COMMUNITY_MODULE_PATH,
@@ -180,11 +181,13 @@ export const APP_ROUTES: Route[] = [
180181
path: 'login',
181182
loadChildren: () => import('./login-page/login-page-routes')
182183
.then((m) => m.ROUTES),
184+
canActivate: [notAuthenticatedGuard],
183185
},
184186
{
185187
path: 'logout',
186188
loadChildren: () => import('./logout-page/logout-page-routes')
187189
.then((m) => m.ROUTES),
190+
canActivate: [authenticatedGuard],
188191
},
189192
{
190193
path: 'submit',
@@ -272,6 +275,7 @@ export const APP_ROUTES: Route[] = [
272275
{
273276
path: 'external-login/:token',
274277
loadChildren: () => import('./external-login-page/external-login-routes').then((m) => m.ROUTES),
278+
canActivate: [notAuthenticatedGuard],
275279
},
276280
{
277281
path: 'review-account/:token',
@@ -282,6 +286,7 @@ export const APP_ROUTES: Route[] = [
282286
path: 'email-confirmation',
283287
loadChildren: () => import('./external-login-email-confirmation-page/external-login-email-confirmation-page-routes')
284288
.then((m) => m.ROUTES),
289+
canActivate: [notAuthenticatedGuard],
285290
},
286291
{ path: '**', pathMatch: 'full', component: ThemedPageNotFoundComponent },
287292
],
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import { TestBed } from '@angular/core/testing';
2+
import {
3+
ActivatedRouteSnapshot,
4+
RouterStateSnapshot,
5+
} from '@angular/router';
6+
import {
7+
firstValueFrom,
8+
of,
9+
} from 'rxjs';
10+
import { PAGE_NOT_FOUND_PATH } from 'src/app/app-routing-paths';
11+
12+
import { HardRedirectService } from '../services/hard-redirect.service';
13+
import { AuthService } from './auth.service';
14+
import { notAuthenticatedGuard } from './not-authenticated.guard';
15+
16+
describe('notAuthenticatedGuard', () => {
17+
let authService: jasmine.SpyObj<AuthService>;
18+
let hardRedirectService: jasmine.SpyObj<HardRedirectService>;
19+
const mockRoute = {} as ActivatedRouteSnapshot;
20+
const mockState = {} as RouterStateSnapshot;
21+
22+
beforeEach(() => {
23+
const authSpy = jasmine.createSpyObj('AuthService', ['isAuthenticated']);
24+
const redirectSpy = jasmine.createSpyObj('HardRedirectService', ['redirect']);
25+
26+
TestBed.configureTestingModule({
27+
providers: [
28+
{ provide: AuthService, useValue: authSpy },
29+
{ provide: HardRedirectService, useValue: redirectSpy },
30+
],
31+
});
32+
33+
authService = TestBed.inject(AuthService) as jasmine.SpyObj<AuthService>;
34+
hardRedirectService = TestBed.inject(HardRedirectService) as jasmine.SpyObj<HardRedirectService>;
35+
});
36+
37+
it('should block access and redirect if user is logged in', async () => {
38+
authService.isAuthenticated.and.returnValue(of(true));
39+
40+
const result$ = TestBed.runInInjectionContext(() =>
41+
notAuthenticatedGuard(mockRoute, mockState),
42+
);
43+
44+
const result = await firstValueFrom(result$ as any);
45+
expect(result).toBe(false);
46+
expect(hardRedirectService.redirect).toHaveBeenCalledWith(PAGE_NOT_FOUND_PATH);
47+
});
48+
49+
it('should allow access if user is not logged in', async () => {
50+
authService.isAuthenticated.and.returnValue(of(false));
51+
52+
const result$ = TestBed.runInInjectionContext(() =>
53+
notAuthenticatedGuard(mockRoute, mockState),
54+
);
55+
56+
const result = await firstValueFrom(result$ as any);
57+
expect(result).toBe(true);
58+
expect(hardRedirectService.redirect).not.toHaveBeenCalled();
59+
});
60+
});
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { inject } from '@angular/core';
2+
import { CanActivateFn } from '@angular/router';
3+
import { map } from 'rxjs/operators';
4+
import { PAGE_NOT_FOUND_PATH } from 'src/app/app-routing-paths';
5+
6+
import { HardRedirectService } from '../services/hard-redirect.service';
7+
import { AuthService } from './auth.service';
8+
9+
export const notAuthenticatedGuard: CanActivateFn = () => {
10+
const authService = inject(AuthService);
11+
const redirectService = inject(HardRedirectService);
12+
13+
return authService.isAuthenticated().pipe(
14+
map((isLoggedIn) => {
15+
if (isLoggedIn) {
16+
redirectService.redirect(PAGE_NOT_FOUND_PATH);
17+
return false;
18+
}
19+
20+
return true;
21+
}),
22+
);
23+
};

0 commit comments

Comments
 (0)