Skip to content

Commit eef6bbe

Browse files
authored
Merge pull request #3983 from atmire/no-platform-specific-code-in-abstract-services-7_x
No platform specific code in abstract services 7_x
2 parents d65ff20 + ddd7c32 commit eef6bbe

File tree

5 files changed

+79
-34
lines changed

5 files changed

+79
-34
lines changed

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ describe('AuthService test', () => {
260260
(state as any).core = Object.create({});
261261
(state as any).core.auth = authenticatedState;
262262
});
263-
authService = new AuthService({}, window, undefined, authReqService, mockEpersonDataService, router, routeService, cookieService, store, hardRedirectService, notificationsService, translateService);
263+
authService = new AuthService(window, authReqService, mockEpersonDataService, router, routeService, cookieService, store, hardRedirectService, notificationsService, translateService);
264264
}));
265265

266266
it('should return true when user is logged in', () => {
@@ -345,7 +345,7 @@ describe('AuthService test', () => {
345345
(state as any).core = Object.create({});
346346
(state as any).core.auth = authenticatedState;
347347
});
348-
authService = new AuthService({}, window, undefined, authReqService, mockEpersonDataService, router, routeService, cookieService, store, hardRedirectService, notificationsService, translateService);
348+
authService = new AuthService(window, authReqService, mockEpersonDataService, router, routeService, cookieService, store, hardRedirectService, notificationsService, translateService);
349349
storage = (authService as any).storage;
350350
routeServiceMock = TestBed.inject(RouteService);
351351
routerStub = TestBed.inject(Router);
@@ -565,7 +565,7 @@ describe('AuthService test', () => {
565565
(state as any).core = Object.create({});
566566
(state as any).core.auth = unAuthenticatedState;
567567
});
568-
authService = new AuthService({}, window, undefined, authReqService, mockEpersonDataService, router, routeService, cookieService, store, hardRedirectService, notificationsService, translateService);
568+
authService = new AuthService(window, authReqService, mockEpersonDataService, router, routeService, cookieService, store, hardRedirectService, notificationsService, translateService);
569569
}));
570570

571571
it('should return null for the shortlived token', () => {
@@ -605,7 +605,7 @@ describe('AuthService test', () => {
605605
(state as any).core = Object.create({});
606606
(state as any).core.auth = idleState;
607607
});
608-
authService = new AuthService({}, window, undefined, authReqService, mockEpersonDataService, router, routeService, cookieService, store, hardRedirectService, notificationsService, translateService);
608+
authService = new AuthService(window, authReqService, mockEpersonDataService, router, routeService, cookieService, store, hardRedirectService, notificationsService, translateService);
609609
}));
610610

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

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

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
import { Inject, Injectable, Optional } from '@angular/core';
1+
import { Inject, Injectable } from '@angular/core';
22
import { Router } from '@angular/router';
33
import { HttpHeaders } from '@angular/common/http';
4-
import { REQUEST, RESPONSE } from '@nguniversal/express-engine/tokens';
54

65
import { Observable, of as observableOf } from 'rxjs';
76
import { filter, map, startWith, switchMap, take } from 'rxjs/operators';
@@ -79,18 +78,17 @@ export class AuthService {
7978
*/
8079
private tokenRefreshTimer;
8180

82-
constructor(@Inject(REQUEST) protected req: any,
83-
@Inject(NativeWindowService) protected _window: NativeWindowRef,
84-
@Optional() @Inject(RESPONSE) private response: any,
85-
protected authRequestService: AuthRequestService,
86-
protected epersonService: EPersonDataService,
87-
protected router: Router,
88-
protected routeService: RouteService,
89-
protected storage: CookieService,
90-
protected store: Store<AppState>,
91-
protected hardRedirectService: HardRedirectService,
92-
private notificationService: NotificationsService,
93-
private translateService: TranslateService
81+
constructor(
82+
@Inject(NativeWindowService) protected _window: NativeWindowRef,
83+
protected authRequestService: AuthRequestService,
84+
protected epersonService: EPersonDataService,
85+
protected router: Router,
86+
protected routeService: RouteService,
87+
protected storage: CookieService,
88+
protected store: Store<AppState>,
89+
protected hardRedirectService: HardRedirectService,
90+
protected notificationService: NotificationsService,
91+
protected translateService: TranslateService
9492
) {
9593
this.store.pipe(
9694
// when this service is constructed the store is not fully initialized yet
@@ -473,10 +471,6 @@ export class AuthService {
473471
if (this._window.nativeWindow.location) {
474472
// Hard redirect to login page, so that all state is definitely lost
475473
this._window.nativeWindow.location.href = redirectUrl;
476-
} else if (this.response) {
477-
if (!this.response._headerSent) {
478-
this.response.redirect(302, redirectUrl);
479-
}
480474
} else {
481475
this.router.navigateByUrl(redirectUrl);
482476
}

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

Lines changed: 56 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,60 @@
1-
import { Injectable } from '@angular/core';
1+
import { Injectable, Inject, Optional } from '@angular/core';
22
import { HttpHeaders } from '@angular/common/http';
3-
3+
import { REQUEST, RESPONSE } from '@nguniversal/express-engine/tokens';
44
import { Observable } from 'rxjs';
55
import { map } from 'rxjs/operators';
6-
76
import { hasValue, isNotEmpty } from '../../shared/empty.util';
87
import { HttpOptions } from '../dspace-rest/dspace-rest.service';
9-
import { AuthService } from './auth.service';
8+
import { AuthService, LOGIN_ROUTE } from './auth.service';
109
import { AuthStatus } from './models/auth-status.model';
1110
import { AuthTokenInfo } from './models/auth-token-info.model';
1211
import { RemoteData } from '../data/remote-data';
12+
import { NativeWindowService, NativeWindowRef } from '../services/window.service';
13+
import { AuthRequestService } from './auth-request.service';
14+
import { EPersonDataService } from '../eperson/eperson-data.service';
15+
import { Router } from '@angular/router';
16+
import { RouteService } from '../services/route.service';
17+
import { CookieService } from '../services/cookie.service';
18+
import { Store } from '@ngrx/store';
19+
import { AppState } from '../../app.reducer';
20+
import { HardRedirectService } from '../services/hard-redirect.service';
21+
import { NotificationsService } from '../../shared/notifications/notifications.service';
22+
import { TranslateService } from '@ngx-translate/core';
1323

1424
/**
1525
* The auth service.
1626
*/
1727
@Injectable()
1828
export class ServerAuthService extends AuthService {
1929

30+
constructor(
31+
@Inject(REQUEST) protected req: any,
32+
@Optional() @Inject(RESPONSE) private response: any,
33+
@Inject(NativeWindowService) protected _window: NativeWindowRef,
34+
protected authRequestService: AuthRequestService,
35+
protected epersonService: EPersonDataService,
36+
protected router: Router,
37+
protected routeService: RouteService,
38+
protected storage: CookieService,
39+
protected store: Store<AppState>,
40+
protected hardRedirectService: HardRedirectService,
41+
protected notificationService: NotificationsService,
42+
protected translateService: TranslateService
43+
) {
44+
super(
45+
_window,
46+
authRequestService,
47+
epersonService,
48+
router,
49+
routeService,
50+
storage,
51+
store,
52+
hardRedirectService,
53+
notificationService,
54+
translateService
55+
);
56+
}
57+
2058
/**
2159
* Returns the authenticated user
2260
* @returns {User}
@@ -60,4 +98,18 @@ export class ServerAuthService extends AuthService {
6098
map((rd: RemoteData<AuthStatus>) => Object.assign(new AuthStatus(), rd.payload))
6199
);
62100
}
101+
102+
override redirectToLoginWhenTokenExpired() {
103+
const redirectUrl = LOGIN_ROUTE + '?expired=true';
104+
if (this._window.nativeWindow.location) {
105+
// Hard redirect to login page, so that all state is definitely lost
106+
this._window.nativeWindow.location.href = redirectUrl;
107+
} else if (this.response) {
108+
if (!this.response._headerSent) {
109+
this.response.redirect(302, redirectUrl);
110+
}
111+
} else {
112+
this.router.navigateByUrl(redirectUrl);
113+
}
114+
}
63115
}

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

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
import { Inject, Injectable } from '@angular/core';
2-
3-
import { REQUEST } from '@nguniversal/express-engine/tokens';
4-
1+
import { Injectable } from '@angular/core';
52
import { Subject , Observable } from 'rxjs';
63
import { CookieAttributes } from 'js-cookie';
74

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

25-
constructor(@Inject(REQUEST) protected req: any) {
26-
}
27-
2822
public abstract set(name: string, value: any, options?: CookieAttributes): void;
2923

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

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
1-
import { Injectable } from '@angular/core';
1+
import { Injectable, Inject } from '@angular/core';
22
import { CookieAttributes } from 'js-cookie';
33
import { CookieService, ICookieService } from './cookie.service';
4+
import { REQUEST } from '@nguniversal/express-engine/tokens';
45

56
@Injectable()
67
export class ServerCookieService extends CookieService implements ICookieService {
78

9+
constructor(@Inject(REQUEST) protected req: any) {
10+
super();
11+
}
12+
813
public set(name: string, value: any, options?: CookieAttributes): void {
914
return;
1015
}

0 commit comments

Comments
 (0)