Skip to content

Commit 28c745e

Browse files
authored
Merge pull request #4901 from 4Science/task/dspace-7_x/DURACOM-390
[Port dspace-7_x] Add route guard to prevent access to register page for authenticated users
2 parents 7271692 + 0fff592 commit 28c745e

File tree

3 files changed

+90
-4
lines changed

3 files changed

+90
-4
lines changed

src/app/app-routing.module.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ import { MenuResolver } from './menu.resolver';
4242
import { ThemedPageErrorComponent } from './page-error/themed-page-error.component';
4343
import { HomePageResolver } from './home-page/home-page.resolver';
4444
import { ViewTrackerResolverService } from './statistics/angulartics/dspace/view-tracker-resolver.service';
45+
import { notAuthenticatedGuard } from './core/auth/not-authenticated.guard';
4546

4647
@NgModule({
4748
imports: [
@@ -98,13 +99,13 @@ import { ViewTrackerResolverService } from './statistics/angulartics/dspace/view
9899
path: REGISTER_PATH,
99100
loadChildren: () => import('./register-page/register-page.module')
100101
.then((m) => m.RegisterPageModule),
101-
canActivate: [SiteRegisterGuard]
102+
canActivate: [notAuthenticatedGuard, SiteRegisterGuard]
102103
},
103104
{
104105
path: FORGOT_PASSWORD_PATH,
105106
loadChildren: () => import('./forgot-password/forgot-password.module')
106107
.then((m) => m.ForgotPasswordModule),
107-
canActivate: [EndUserAgreementCurrentUserGuard]
108+
canActivate: [notAuthenticatedGuard, EndUserAgreementCurrentUserGuard]
108109
},
109110
{
110111
path: COMMUNITY_MODULE_PATH,
@@ -169,12 +170,14 @@ import { ViewTrackerResolverService } from './statistics/angulartics/dspace/view
169170
{
170171
path: 'login',
171172
loadChildren: () => import('./login-page/login-page.module')
172-
.then((m) => m.LoginPageModule)
173+
.then((m) => m.LoginPageModule),
174+
canActivate: [notAuthenticatedGuard]
173175
},
174176
{
175177
path: 'logout',
176178
loadChildren: () => import('./logout-page/logout-page.module')
177-
.then((m) => m.LogoutPageModule)
179+
.then((m) => m.LogoutPageModule),
180+
canActivate: [AuthenticatedGuard]
178181
},
179182
{
180183
path: 'submit',
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)