Skip to content

Commit 57d5bb0

Browse files
authored
Merge pull request #3981 from atmire/no-platform-specific-code-in-abstract-services-main
No platform specific code in abstract services main
2 parents c9654b2 + 3197ec9 commit 57d5bb0

File tree

5 files changed

+94
-37
lines changed

5 files changed

+94
-37
lines changed

src/app/core/auth/auth.service.spec.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ describe('AuthService test', () => {
288288
(state as any).core = Object.create({});
289289
(state as any).core.auth = authenticatedState;
290290
});
291-
authService = new AuthService({}, window, undefined, authReqService, mockEpersonDataService, router, routeService, cookieService, store, hardRedirectService, notificationsService, translateService);
291+
authService = new AuthService(window, authReqService, mockEpersonDataService, router, routeService, cookieService, store, hardRedirectService, notificationsService, translateService);
292292
}));
293293

294294
it('should return true when user is logged in', () => {
@@ -373,7 +373,7 @@ describe('AuthService test', () => {
373373
(state as any).core = Object.create({});
374374
(state as any).core.auth = authenticatedState;
375375
});
376-
authService = new AuthService({}, window, undefined, authReqService, mockEpersonDataService, router, routeService, cookieService, store, hardRedirectService, notificationsService, translateService);
376+
authService = new AuthService(window, authReqService, mockEpersonDataService, router, routeService, cookieService, store, hardRedirectService, notificationsService, translateService);
377377
storage = (authService as any).storage;
378378
routeServiceMock = TestBed.inject(RouteService);
379379
routerStub = TestBed.inject(Router);
@@ -593,7 +593,7 @@ describe('AuthService test', () => {
593593
(state as any).core = Object.create({});
594594
(state as any).core.auth = unAuthenticatedState;
595595
});
596-
authService = new AuthService({}, window, undefined, authReqService, mockEpersonDataService, router, routeService, cookieService, store, hardRedirectService, notificationsService, translateService);
596+
authService = new AuthService(window, authReqService, mockEpersonDataService, router, routeService, cookieService, store, hardRedirectService, notificationsService, translateService);
597597
}));
598598

599599
it('should return null for the shortlived token', () => {
@@ -633,7 +633,7 @@ describe('AuthService test', () => {
633633
(state as any).core = Object.create({});
634634
(state as any).core.auth = idleState;
635635
});
636-
authService = new AuthService({}, window, undefined, authReqService, mockEpersonDataService, router, routeService, cookieService, store, hardRedirectService, notificationsService, translateService);
636+
authService = new AuthService(window, authReqService, mockEpersonDataService, router, routeService, cookieService, store, hardRedirectService, notificationsService, translateService);
637637
}));
638638

639639
it('isUserIdle should return true when user is not idle', () => {

src/app/core/auth/auth.service.ts

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import { HttpHeaders } from '@angular/common/http';
22
import {
33
Inject,
44
Injectable,
5-
Optional,
65
} from '@angular/core';
76
import { Router } from '@angular/router';
87
import {
@@ -24,10 +23,6 @@ import {
2423
} from 'rxjs/operators';
2524

2625
import { environment } from '../../../environments/environment';
27-
import {
28-
REQUEST,
29-
RESPONSE,
30-
} from '../../../express.tokens';
3126
import { AppState } from '../../app.reducer';
3227
import {
3328
hasNoValue,
@@ -112,18 +107,17 @@ export class AuthService {
112107
*/
113108
private tokenRefreshTimer;
114109

115-
constructor(@Inject(REQUEST) protected req: any,
116-
@Inject(NativeWindowService) protected _window: NativeWindowRef,
117-
@Optional() @Inject(RESPONSE) private response: any,
118-
protected authRequestService: AuthRequestService,
119-
protected epersonService: EPersonDataService,
120-
protected router: Router,
121-
protected routeService: RouteService,
122-
protected storage: CookieService,
123-
protected store: Store<AppState>,
124-
protected hardRedirectService: HardRedirectService,
125-
private notificationService: NotificationsService,
126-
private translateService: TranslateService,
110+
constructor(
111+
@Inject(NativeWindowService) protected _window: NativeWindowRef,
112+
protected authRequestService: AuthRequestService,
113+
protected epersonService: EPersonDataService,
114+
protected router: Router,
115+
protected routeService: RouteService,
116+
protected storage: CookieService,
117+
protected store: Store<AppState>,
118+
protected hardRedirectService: HardRedirectService,
119+
protected notificationService: NotificationsService,
120+
protected translateService: TranslateService,
127121
) {
128122
this.store.pipe(
129123
// when this service is constructed the store is not fully initialized yet
@@ -505,10 +499,6 @@ export class AuthService {
505499
if (this._window.nativeWindow.location) {
506500
// Hard redirect to login page, so that all state is definitely lost
507501
this._window.nativeWindow.location.href = redirectUrl;
508-
} else if (this.response) {
509-
if (!this.response._headerSent) {
510-
this.response.redirect(302, redirectUrl);
511-
}
512502
} else {
513503
this.router.navigateByUrl(redirectUrl);
514504
}

src/app/core/auth/server-auth.service.ts

Lines changed: 69 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,40 @@
11
import { HttpHeaders } from '@angular/common/http';
2-
import { Injectable } from '@angular/core';
2+
import {
3+
Inject,
4+
Injectable,
5+
Optional,
6+
} from '@angular/core';
7+
import { Router } from '@angular/router';
8+
import { Store } from '@ngrx/store';
9+
import { TranslateService } from '@ngx-translate/core';
310
import { Observable } from 'rxjs';
411
import { map } from 'rxjs/operators';
512

13+
import {
14+
REQUEST,
15+
RESPONSE,
16+
} from '../../../express.tokens';
17+
import { AppState } from '../../app.reducer';
618
import {
719
hasValue,
820
isNotEmpty,
921
} from '../../shared/empty.util';
22+
import { NotificationsService } from '../../shared/notifications/notifications.service';
1023
import { RemoteData } from '../data/remote-data';
1124
import { HttpOptions } from '../dspace-rest/dspace-rest.service';
12-
import { AuthService } from './auth.service';
25+
import { EPersonDataService } from '../eperson/eperson-data.service';
26+
import { CookieService } from '../services/cookie.service';
27+
import { HardRedirectService } from '../services/hard-redirect.service';
28+
import { RouteService } from '../services/route.service';
29+
import {
30+
NativeWindowRef,
31+
NativeWindowService,
32+
} from '../services/window.service';
33+
import {
34+
AuthService,
35+
LOGIN_ROUTE,
36+
} from './auth.service';
37+
import { AuthRequestService } from './auth-request.service';
1338
import { AuthStatus } from './models/auth-status.model';
1439
import { AuthTokenInfo } from './models/auth-token-info.model';
1540

@@ -19,6 +44,34 @@ import { AuthTokenInfo } from './models/auth-token-info.model';
1944
@Injectable()
2045
export class ServerAuthService extends AuthService {
2146

47+
constructor(
48+
@Inject(REQUEST) protected req: any,
49+
@Optional() @Inject(RESPONSE) private response: any,
50+
@Inject(NativeWindowService) protected _window: NativeWindowRef,
51+
protected authRequestService: AuthRequestService,
52+
protected epersonService: EPersonDataService,
53+
protected router: Router,
54+
protected routeService: RouteService,
55+
protected storage: CookieService,
56+
protected store: Store<AppState>,
57+
protected hardRedirectService: HardRedirectService,
58+
protected notificationService: NotificationsService,
59+
protected translateService: TranslateService,
60+
) {
61+
super(
62+
_window,
63+
authRequestService,
64+
epersonService,
65+
router,
66+
routeService,
67+
storage,
68+
store,
69+
hardRedirectService,
70+
notificationService,
71+
translateService,
72+
);
73+
}
74+
2275
/**
2376
* Returns the authenticated user
2477
* @returns {User}
@@ -62,4 +115,18 @@ export class ServerAuthService extends AuthService {
62115
map((rd: RemoteData<AuthStatus>) => Object.assign(new AuthStatus(), rd.payload)),
63116
);
64117
}
118+
119+
override redirectToLoginWhenTokenExpired() {
120+
const redirectUrl = LOGIN_ROUTE + '?expired=true';
121+
if (this._window.nativeWindow.location) {
122+
// Hard redirect to login page, so that all state is definitely lost
123+
this._window.nativeWindow.location.href = redirectUrl;
124+
} else if (this.response) {
125+
if (!this.response._headerSent) {
126+
this.response.redirect(302, redirectUrl);
127+
}
128+
} else {
129+
this.router.navigateByUrl(redirectUrl);
130+
}
131+
}
65132
}

src/app/core/services/cookie.service.ts

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,10 @@
1-
import {
2-
Inject,
3-
Injectable,
4-
} from '@angular/core';
1+
import { Injectable } from '@angular/core';
52
import { CookieAttributes } from 'js-cookie';
63
import {
74
Observable,
85
Subject,
96
} from 'rxjs';
107

11-
import { REQUEST } from '../../../express.tokens';
12-
138
export interface ICookieService {
149
readonly cookies$: Observable<{ readonly [key: string]: any }>;
1510

@@ -27,9 +22,6 @@ export abstract class CookieService implements ICookieService {
2722
protected readonly cookieSource = new Subject<{ readonly [key: string]: any }>();
2823
public readonly cookies$ = this.cookieSource.asObservable();
2924

30-
constructor(@Inject(REQUEST) protected req: any) {
31-
}
32-
3325
public abstract set(name: string, value: any, options?: CookieAttributes): void;
3426

3527
public abstract remove(name: string, options?: CookieAttributes): void;

src/app/core/services/server-cookie.service.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
1-
import { Injectable } from '@angular/core';
1+
import {
2+
Inject,
3+
Injectable,
4+
} from '@angular/core';
25
import { CookieAttributes } from 'js-cookie';
36

7+
import { REQUEST } from '../../../express.tokens';
48
import {
59
CookieService,
610
ICookieService,
@@ -9,6 +13,10 @@ import {
913
@Injectable()
1014
export class ServerCookieService extends CookieService implements ICookieService {
1115

16+
constructor(@Inject(REQUEST) protected req: any) {
17+
super();
18+
}
19+
1220
public set(name: string, value: any, options?: CookieAttributes): void {
1321
return;
1422
}

0 commit comments

Comments
 (0)