Skip to content

Commit 2f3c3c7

Browse files
authored
Merge pull request DSpace#4764 from 4Science/task/main/DURACOM-390
Add route guard to prevent access to register page for authenticated users
2 parents b16184c + 9daa92a commit 2f3c3c7

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
@@ -35,6 +35,7 @@ import {
3535
REQUEST_COPY_MODULE_PATH,
3636
WORKFLOW_ITEM_MODULE_PATH,
3737
} from './app-routing-paths';
38+
import { notAuthenticatedGuard } from './core/auth/not-authenticated.guard';
3839
import { ThemedForbiddenComponent } from './forbidden/themed-forbidden.component';
3940
import { homePageResolver } from './home-page/home-page.resolver';
4041
import { provideSuggestionNotificationsState } from './notifications/provide-suggestion-notifications-state';
@@ -99,13 +100,13 @@ export const APP_ROUTES: Route[] = [
99100
path: REGISTER_PATH,
100101
loadChildren: () => import('./register-page/register-page-routes')
101102
.then((m) => m.ROUTES),
102-
canActivate: [siteRegisterGuard],
103+
canActivate: [notAuthenticatedGuard, siteRegisterGuard],
103104
},
104105
{
105106
path: FORGOT_PASSWORD_PATH,
106107
loadChildren: () => import('./forgot-password/forgot-password-routes')
107108
.then((m) => m.ROUTES),
108-
canActivate: [endUserAgreementCurrentUserGuard, forgotPasswordCheckGuard],
109+
canActivate: [notAuthenticatedGuard, endUserAgreementCurrentUserGuard, forgotPasswordCheckGuard],
109110
},
110111
{
111112
path: COMMUNITY_MODULE_PATH,
@@ -182,11 +183,13 @@ export const APP_ROUTES: Route[] = [
182183
path: 'login',
183184
loadChildren: () => import('./login-page/login-page-routes')
184185
.then((m) => m.ROUTES),
186+
canActivate: [notAuthenticatedGuard],
185187
},
186188
{
187189
path: 'logout',
188190
loadChildren: () => import('./logout-page/logout-page-routes')
189191
.then((m) => m.ROUTES),
192+
canActivate: [authenticatedGuard],
190193
},
191194
{
192195
path: 'submit',
@@ -274,6 +277,7 @@ export const APP_ROUTES: Route[] = [
274277
{
275278
path: 'external-login/:token',
276279
loadChildren: () => import('./external-login-page/external-login-routes').then((m) => m.ROUTES),
280+
canActivate: [notAuthenticatedGuard],
277281
},
278282
{
279283
path: 'review-account/:token',
@@ -284,6 +288,7 @@ export const APP_ROUTES: Route[] = [
284288
path: 'email-confirmation',
285289
loadChildren: () => import('./external-login-email-confirmation-page/external-login-email-confirmation-page-routes')
286290
.then((m) => m.ROUTES),
291+
canActivate: [notAuthenticatedGuard],
287292
},
288293
{ path: '**', pathMatch: 'full', component: ThemedPageNotFoundComponent },
289294
],
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 '../router/core-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 '../router/core-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)