125969: Get rid of REQUEST and RESPONSE imports in AuthService

This commit is contained in:
Andreas Awouters
2025-02-05 16:05:48 +01:00
parent 8b90d999ac
commit f82426b1dc
2 changed files with 68 additions and 22 deletions

View File

@@ -1,7 +1,6 @@
import { Inject, Injectable, Optional } from '@angular/core';
import { Inject, Injectable } from '@angular/core';
import { Router } from '@angular/router';
import { HttpHeaders } from '@angular/common/http';
import { REQUEST, RESPONSE } from '@nguniversal/express-engine/tokens';
import { Observable, of as observableOf } from 'rxjs';
import { filter, map, startWith, switchMap, take } from 'rxjs/operators';
@@ -79,18 +78,17 @@ export class AuthService {
*/
private tokenRefreshTimer;
constructor(@Inject(REQUEST) protected req: any,
@Inject(NativeWindowService) protected _window: NativeWindowRef,
@Optional() @Inject(RESPONSE) private response: any,
protected authRequestService: AuthRequestService,
protected epersonService: EPersonDataService,
protected router: Router,
protected routeService: RouteService,
protected storage: CookieService,
protected store: Store<AppState>,
protected hardRedirectService: HardRedirectService,
private notificationService: NotificationsService,
private translateService: TranslateService
constructor(
@Inject(NativeWindowService) protected _window: NativeWindowRef,
protected authRequestService: AuthRequestService,
protected epersonService: EPersonDataService,
protected router: Router,
protected routeService: RouteService,
protected storage: CookieService,
protected store: Store<AppState>,
protected hardRedirectService: HardRedirectService,
protected notificationService: NotificationsService,
protected translateService: TranslateService
) {
this.store.pipe(
// when this service is constructed the store is not fully initialized yet
@@ -473,10 +471,6 @@ export class AuthService {
if (this._window.nativeWindow.location) {
// Hard redirect to login page, so that all state is definitely lost
this._window.nativeWindow.location.href = redirectUrl;
} else if (this.response) {
if (!this.response._headerSent) {
this.response.redirect(302, redirectUrl);
}
} else {
this.router.navigateByUrl(redirectUrl);
}

View File

@@ -1,15 +1,25 @@
import { Injectable } from '@angular/core';
import { Injectable, Inject, Optional } from '@angular/core';
import { HttpHeaders } from '@angular/common/http';
import { REQUEST, RESPONSE } from '@nguniversal/express-engine/tokens';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';
import { hasValue, isNotEmpty } from '../../shared/empty.util';
import { HttpOptions } from '../dspace-rest/dspace-rest.service';
import { AuthService } from './auth.service';
import { AuthService, LOGIN_ROUTE } from './auth.service';
import { AuthStatus } from './models/auth-status.model';
import { AuthTokenInfo } from './models/auth-token-info.model';
import { RemoteData } from '../data/remote-data';
import { NativeWindowService, NativeWindowRef } from '../services/window.service';
import { AuthRequestService } from './auth-request.service';
import { EPersonDataService } from '../eperson/eperson-data.service';
import { Router } from '@angular/router';
import { RouteService } from '../services/route.service';
import { CookieService } from '../services/cookie.service';
import { Store } from '@ngrx/store';
import { AppState } from '../../app.reducer';
import { HardRedirectService } from '../services/hard-redirect.service';
import { NotificationsService } from '../../shared/notifications/notifications.service';
import { TranslateService } from '@ngx-translate/core';
/**
* The auth service.
@@ -17,6 +27,34 @@ import { RemoteData } from '../data/remote-data';
@Injectable()
export class ServerAuthService extends AuthService {
constructor(
@Inject(REQUEST) protected req: any,
@Optional() @Inject(RESPONSE) private response: any,
@Inject(NativeWindowService) protected _window: NativeWindowRef,
protected authRequestService: AuthRequestService,
protected epersonService: EPersonDataService,
protected router: Router,
protected routeService: RouteService,
protected storage: CookieService,
protected store: Store<AppState>,
protected hardRedirectService: HardRedirectService,
protected notificationService: NotificationsService,
protected translateService: TranslateService
) {
super(
_window,
authRequestService,
epersonService,
router,
routeService,
storage,
store,
hardRedirectService,
notificationService,
translateService
);
}
/**
* Returns the authenticated user
* @returns {User}
@@ -60,4 +98,18 @@ export class ServerAuthService extends AuthService {
map((rd: RemoteData<AuthStatus>) => Object.assign(new AuthStatus(), rd.payload))
);
}
override redirectToLoginWhenTokenExpired() {
const redirectUrl = LOGIN_ROUTE + '?expired=true';
if (this._window.nativeWindow.location) {
// Hard redirect to login page, so that all state is definitely lost
this._window.nativeWindow.location.href = redirectUrl;
} else if (this.response) {
if (!this.response._headerSent) {
this.response.redirect(302, redirectUrl);
}
} else {
this.router.navigateByUrl(redirectUrl);
}
}
}