|
1 | | -import { Injectable } from '@angular/core'; |
| 1 | +import { Injectable, Inject, Optional } from '@angular/core'; |
2 | 2 | import { HttpHeaders } from '@angular/common/http'; |
3 | | - |
| 3 | +import { REQUEST, RESPONSE } from '@nguniversal/express-engine/tokens'; |
4 | 4 | import { Observable } from 'rxjs'; |
5 | 5 | import { map } from 'rxjs/operators'; |
6 | | - |
7 | 6 | import { hasValue, isNotEmpty } from '../../shared/empty.util'; |
8 | 7 | import { HttpOptions } from '../dspace-rest/dspace-rest.service'; |
9 | | -import { AuthService } from './auth.service'; |
| 8 | +import { AuthService, LOGIN_ROUTE } from './auth.service'; |
10 | 9 | import { AuthStatus } from './models/auth-status.model'; |
11 | 10 | import { AuthTokenInfo } from './models/auth-token-info.model'; |
12 | 11 | 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'; |
13 | 23 |
|
14 | 24 | /** |
15 | 25 | * The auth service. |
16 | 26 | */ |
17 | 27 | @Injectable() |
18 | 28 | export class ServerAuthService extends AuthService { |
19 | 29 |
|
| 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 | + |
20 | 58 | /** |
21 | 59 | * Returns the authenticated user |
22 | 60 | * @returns {User} |
@@ -60,4 +98,18 @@ export class ServerAuthService extends AuthService { |
60 | 98 | map((rd: RemoteData<AuthStatus>) => Object.assign(new AuthStatus(), rd.payload)) |
61 | 99 | ); |
62 | 100 | } |
| 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 | + } |
63 | 115 | } |
0 commit comments