mirror of
https://github.com/DSpace/dspace-angular.git
synced 2025-10-07 01:54:15 +00:00
force initservices to wait until authentication is no longer blocking
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import { Observable } from 'rxjs';
|
||||
import { distinctUntilChanged, filter, map, mergeMap, switchMap, tap } from 'rxjs/operators';
|
||||
import { distinctUntilChanged, filter, map, mergeMap, switchMap, tap, take } from 'rxjs/operators';
|
||||
import { HALEndpointService } from '../shared/hal-endpoint.service';
|
||||
import { RequestService } from '../data/request.service';
|
||||
import { isNotEmpty } from '../../shared/empty.util';
|
||||
@@ -26,8 +26,8 @@ export abstract class AuthRequestService {
|
||||
) {
|
||||
}
|
||||
|
||||
protected fetchRequest(request: RestRequest): Observable<RemoteData<AuthStatus>> {
|
||||
return this.rdbService.buildFromRequestUUID<AuthStatus>(request.uuid).pipe(
|
||||
protected fetchRequest(requestId: string): Observable<RemoteData<AuthStatus>> {
|
||||
return this.rdbService.buildFromRequestUUID<AuthStatus>(requestId).pipe(
|
||||
getFirstCompletedRemoteData(),
|
||||
);
|
||||
}
|
||||
@@ -37,27 +37,36 @@ export abstract class AuthRequestService {
|
||||
}
|
||||
|
||||
public postToEndpoint(method: string, body?: any, options?: HttpOptions): Observable<RemoteData<AuthStatus>> {
|
||||
return this.halService.getEndpoint(this.linkName).pipe(
|
||||
const requestId = this.requestService.generateRequestId();
|
||||
|
||||
this.halService.getEndpoint(this.linkName).pipe(
|
||||
filter((href: string) => isNotEmpty(href)),
|
||||
map((endpointURL) => this.getEndpointByMethod(endpointURL, method)),
|
||||
distinctUntilChanged(),
|
||||
map((endpointURL: string) => new PostRequest(this.requestService.generateRequestId(), endpointURL, body, options)),
|
||||
tap((request: PostRequest) => this.requestService.send(request)),
|
||||
mergeMap((request: PostRequest) => this.fetchRequest(request)),
|
||||
distinctUntilChanged());
|
||||
map((endpointURL: string) => new PostRequest(requestId, endpointURL, body, options)),
|
||||
take(1)
|
||||
).subscribe((request: PostRequest) => {
|
||||
this.requestService.send(request);
|
||||
});
|
||||
|
||||
return this.fetchRequest(requestId);
|
||||
}
|
||||
|
||||
public getRequest(method: string, options?: HttpOptions): Observable<RemoteData<AuthStatus>> {
|
||||
return this.halService.getEndpoint(this.linkName).pipe(
|
||||
const requestId = this.requestService.generateRequestId();
|
||||
|
||||
this.halService.getEndpoint(this.linkName).pipe(
|
||||
filter((href: string) => isNotEmpty(href)),
|
||||
map((endpointURL) => this.getEndpointByMethod(endpointURL, method)),
|
||||
distinctUntilChanged(),
|
||||
map((endpointURL: string) => new GetRequest(this.requestService.generateRequestId(), endpointURL, undefined, options)),
|
||||
tap((request: GetRequest) => this.requestService.send(request)),
|
||||
mergeMap((request: GetRequest) => this.fetchRequest(request)),
|
||||
distinctUntilChanged());
|
||||
}
|
||||
map((endpointURL: string) => new GetRequest(requestId, endpointURL, undefined, options)),
|
||||
take(1)
|
||||
).subscribe((request: GetRequest) => {
|
||||
this.requestService.send(request);
|
||||
});
|
||||
|
||||
return this.fetchRequest(requestId);
|
||||
}
|
||||
/**
|
||||
* Factory function to create the request object to send. This needs to be a POST client side and
|
||||
* a GET server side. Due to CSRF validation, the server isn't allowed to send a POST, so we allow
|
||||
|
@@ -17,6 +17,7 @@ import {
|
||||
import { AuthTokenInfo } from './models/auth-token-info.model';
|
||||
import { AuthMethod } from './models/auth.method';
|
||||
import { AuthMethodType } from './models/auth.method-type';
|
||||
import { StoreActionTypes } from '../../store.actions';
|
||||
|
||||
/**
|
||||
* The auth state.
|
||||
@@ -251,6 +252,11 @@ export function authReducer(state: any = initialState, action: AuthActions): Aut
|
||||
idle: false,
|
||||
});
|
||||
|
||||
case StoreActionTypes.REHYDRATE:
|
||||
return Object.assign({}, state, {
|
||||
blocking: true,
|
||||
});
|
||||
|
||||
default:
|
||||
return state;
|
||||
}
|
||||
|
@@ -91,6 +91,13 @@ export class BrowserInitService extends InitService {
|
||||
|
||||
this.initKlaro();
|
||||
|
||||
// wait for auth to be ready
|
||||
await this.store.pipe(
|
||||
select(isAuthenticationBlocking),
|
||||
distinctUntilChanged(),
|
||||
find((b: boolean) => b === false)
|
||||
).toPromise();
|
||||
|
||||
return true;
|
||||
};
|
||||
}
|
||||
|
@@ -6,7 +6,7 @@
|
||||
* http://www.dspace.org/license/
|
||||
*/
|
||||
import { InitService } from '../../app/init.service';
|
||||
import { Store } from '@ngrx/store';
|
||||
import { Store, select } from '@ngrx/store';
|
||||
import { AppState } from '../../app/app.reducer';
|
||||
import { TransferState } from '@angular/platform-browser';
|
||||
import { CorrelationIdService } from '../../app/correlation-id/correlation-id.service';
|
||||
@@ -20,7 +20,8 @@ import { MetadataService } from '../../app/core/metadata/metadata.service';
|
||||
import { BreadcrumbsService } from '../../app/breadcrumbs/breadcrumbs.service';
|
||||
import { CSSVariableService } from '../../app/shared/sass-helper/sass-helper.service';
|
||||
import { ThemeService } from '../../app/shared/theme-support/theme.service';
|
||||
import { take } from 'rxjs/operators';
|
||||
import { take, distinctUntilChanged, find } from 'rxjs/operators';
|
||||
import { isAuthenticationBlocking } from '../../app/core/auth/selectors';
|
||||
|
||||
/**
|
||||
* Performs server-side initialization.
|
||||
@@ -66,6 +67,13 @@ export class ServerInitService extends InitService {
|
||||
this.initRouteListeners();
|
||||
this.themeService.listenForThemeChanges(false);
|
||||
|
||||
// wait for auth to be ready
|
||||
await this.store.pipe(
|
||||
select(isAuthenticationBlocking),
|
||||
distinctUntilChanged(),
|
||||
find((b: boolean) => b === false)
|
||||
).toPromise();
|
||||
|
||||
return true;
|
||||
};
|
||||
}
|
||||
|
Reference in New Issue
Block a user