Skip to content

Commit 67a0395

Browse files
[DURACOM-390] implement not authenticated guard
1 parent 27a1bfc commit 67a0395

3 files changed

Lines changed: 85 additions & 1 deletion

File tree

src/app/app-routes.ts

Lines changed: 2 additions & 1 deletion
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,7 +98,7 @@ 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,
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+
11+
import { PAGE_NOT_FOUND_PATH } from '../../app-routing-paths';
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+
5+
import { PAGE_NOT_FOUND_PATH } from '../../app-routing-paths';
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)